- เพิ่ม Reference
- สร้างไฟล์ Excel เปล่า
- กำหนดข้อความ ตัวหนา และตำแหน่งกึ่งกลาง
- การใช้ Array การใส่สูตร การจัดขนาดคอลัมน์
- เพิ่ม WorkSheet และจัดลำดับ WorkSheet
- โค๊ดทั้งหมด
- How to automate Microsoft Excel from Microsoft Visual C#.NET
- How to: Programmatically save workbooks
- How to: Programmatically move worksheets within workbooks
1. เพิ่ม Reference
Microsoft.Office.Interop.Excel
2.สร้างไฟล์ Excel เปล่า
สร้าง Application และ Workbook จากนั้นสั่ง save ที่ Workbook
using System; using System.Reflection; using Microsoft.Office.Interop.Excel; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Application excApp; Workbook excBook; try { //Start Microsoft.Office.Interop.Excel and get Application object. excApp = new Application(); excApp.Visible = false; excApp.UserControl = false; //Get a new workbook. excBook = (Workbook)(excApp.Workbooks.Add(Missing.Value)); // Save file excBook.SaveAs(@"d:\Book1.xlsx"); excBook.Close(); } catch (Exception ex) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, ex.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, ex.Source); Console.WriteLine(errorMessage); } } } }
3.กำหนดข้อความ ตัวหนา และตำแหน่งกึ่งกลาง
using System; using System.Reflection; using Microsoft.Office.Interop.Excel; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Application excApp; Workbook excBook; Worksheet excSheet; try { //Start Microsoft.Office.Interop.Excel and get Application object. excApp = new Application(); excApp.Visible = false; excApp.UserControl = false; //Get a new workbook. excBook = (Workbook)(excApp.Workbooks.Add(Missing.Value)); excSheet = (Worksheet)excBook.ActiveSheet; //Add table headers going cell by cell. excSheet.Cells[1, 1] = "First Name"; excSheet.Cells[1, 2] = "Last Name"; excSheet.Cells[1, 3] = "Full Name"; excSheet.Cells[1, 4] = "Salary"; //Format A1:D1 as bold excSheet.get_Range("A1", "D1").Font.Bold = true; //Format A1:D1 as horizontal alignment = center. excSheet.get_Range("A1", "D1").HorizontalAlignment = XlHAlign.xlHAlignCenter; //Format A1:D1 as vertical alignment = center. excSheet.get_Range("A1", "D1").VerticalAlignment = XlVAlign.xlVAlignCenter; // Save file excBook.SaveAs(@"d:\Book1.xlsx"); excBook.Close(); } catch (Exception ex) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, ex.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, ex.Source); Console.WriteLine(errorMessage); } } } }
บรรทัดที่ 27-30 กำหนดข้อความ
บรรทัดที่ 33 กำหนดให้เป็นตัวหนา
บรรทัดที่ 36 กำหนดให้กึ่งกลางในแนวนอน
บรรทัดที่ 40 กำหนดให้กึ่งกลางในแนวตั้ง
ลองขยายขนาดของ cell ออกมาดู จะเห็นชัดว่าอยู่กึ่งกลางทั้งแนวนอนและแนวตั้ง
4.การใช้ Array การใส่สูตร การจัดขนาดคอลัมน์
เพิ่มข้อความแบบ Array
... // Create an array to multiple values at once. string[,] saNames = new string[5, 2]; saNames[0, 0] = "John"; saNames[0, 1] = "Smith"; saNames[1, 0] = "Tom"; saNames[1, 1] = "Brown"; saNames[2, 0] = "Sue"; saNames[2, 1] = "Thomas"; saNames[3, 0] = "Jane"; saNames[3, 1] = "Jones"; saNames[4, 0] = "Adam"; saNames[4, 1] = "Johnson"; //Fill A2:B6 with an array of values (First and Last Names). excSheet.get_Range("A2", "B6").Value2 = saNames; ...
ใส่สูตรของ Excel และกำหนดขนาดคอลัมน์อัตโนมัติด้วย Range
Range excRange; ... ... //Fill A2:B6 with an array of values (First and Last Names). excSheet.get_Range("A2", "B6").Value2 = saNames; //Fill C2:C6 with a relative formula (=A2 & " " & B2). excRange = excSheet.get_Range("C2", "C6"); excRange.Formula = "=A2 & \" \" & B2"; //Fill D2:D6 with a formula(=RAND()*100000) and apply format. excRange = excSheet.get_Range("D2", "D6"); excRange.Formula = "=RAND()*100000"; excRange.NumberFormat = "$0.00"; //AutoFit columns A:D. excRange = excSheet.get_Range("A1", "D1"); excRange.EntireColumn.AutoFit(); ...
บรรทัดที่ 11 ดึงข้อมูลจาก cell อื่นมาใส่
บรรทัดที่ 15 ใช้ฟังก์ชันของ Excel
บรรทัดที่ 16 จัด format เซลล์
บรรทัดที่ 20 จัดขนาดคอลัมน์ให้พอดีกับข้อความอัตโนมัติ
5.เพิ่ม WorkSheet และจัดลำดับ WorkSheet
เพิ่ม 2 WorkSheet คือ excSheet2
และ excSheet3
เรียงลำดับ WorkSheet โดยนำมาแทรกอยู่ด้านหน้า (ลองนำไปต่อท้ายแล้วใช้ไมไ่ด้)
Worksheet excSheet2; Worksheet excSheet3; ... ... excSheet2 = (Worksheet)excBook.Worksheets.Add(Missing.Value); excSheet2.Cells[1, 1] = "Sheet2"; excSheet3 = (Worksheet)excBook.Worksheets.Add(Missing.Value); excSheet3.Cells[1, 1] = "Sheet3"; // rearrange worksheet excSheet2.Move(excBook.Sheets[1]); excSheet.Move(excBook.Sheets[1]); ...
6.โค๊ดทั้งหมด
using System; using System.Reflection; using Microsoft.Office.Interop.Excel; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Application excApp; Workbook excBook; Worksheet excSheet; Worksheet excSheet2; Worksheet excSheet3; Range excRange; try { //Start Microsoft.Office.Interop.Excel and get Application object. excApp = new Application(); excApp.Visible = false; excApp.UserControl = false; //Get a new workbook. excBook = (Workbook)(excApp.Workbooks.Add(Missing.Value)); excSheet = (Worksheet)excBook.ActiveSheet; //Add table headers going cell by cell. excSheet.Cells[1, 1] = "First Name"; excSheet.Cells[1, 2] = "Last Name"; excSheet.Cells[1, 3] = "Full Name"; excSheet.Cells[1, 4] = "Salary"; //Format A1:D1 as bold excSheet.get_Range("A1", "D1").Font.Bold = true; //Format A1:D1 as horizontal alignment = center. excSheet.get_Range("A1", "D1").HorizontalAlignment = XlHAlign.xlHAlignCenter; //Format A1:D1 as vertical alignment = center. excSheet.get_Range("A1", "D1").VerticalAlignment = XlVAlign.xlVAlignCenter; // Create an array to multiple values at once. string[,] saNames = new string[5, 2]; saNames[0, 0] = "John"; saNames[0, 1] = "Smith"; saNames[1, 0] = "Tom"; saNames[1, 1] = "Brown"; saNames[2, 0] = "Sue"; saNames[2, 1] = "Thomas"; saNames[3, 0] = "Jane"; saNames[3, 1] = "Jones"; saNames[4, 0] = "Adam"; saNames[4, 1] = "Johnson"; //Fill A2:B6 with an array of values (First and Last Names). excSheet.get_Range("A2", "B6").Value2 = saNames; //Fill C2:C6 with a relative formula (=A2 & " " & B2). excRange = excSheet.get_Range("C2", "C6"); excRange.Formula = "=A2 & \" \" & B2"; //Fill D2:D6 with a formula(=RAND()*100000) and apply format. excRange = excSheet.get_Range("D2", "D6"); excRange.Formula = "=RAND()*100000"; excRange.NumberFormat = "$0.00"; //AutoFit columns A:D. excRange = excSheet.get_Range("A1", "D1"); excRange.EntireColumn.AutoFit(); excSheet2 = (Worksheet)excBook.Worksheets.Add(Missing.Value); excSheet2.Cells[1, 1] = "Sheet2"; excSheet3 = (Worksheet)excBook.Worksheets.Add(Missing.Value); excSheet3.Cells[1, 1] = "Sheet3"; excSheet2.Move(excBook.Sheets[1]); excSheet.Move(excBook.Sheets[1]); // Save file excBook.SaveAs(@"d:\Book1.xlsx"); excBook.Close(); } catch (Exception ex) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, ex.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, ex.Source); Console.WriteLine(errorMessage); } } } }