- Pada Microsoft Visual Studio 2010, buat New Project dengan Console Application template.
- Pilih Add Reference dan tambahkan library ExcelPackage, yaitu: ExcelPackage.dll ke dalam project.
- Definisikan sebuah method untuk membuat xlsx file.
1: /// <summary>
2: /// Membuat sebuah xlsx file.
3: /// </summary>
4: private static void CreateXlsx()
5: { 6: FileInfo file = new FileInfo(@"testfile.xlsx");
7: ExcelPackage xlsx = new ExcelPackage(file);
8:
9: FillData(xlsx);
10: SetDocumentProperties(xlsx);
11: //Menyimpan dalam bentuk xlsx file dengan nama file yang telah ditentukan
12: xlsx.Save();
13: }
- Definisikan method FillData yang dipanggil di dalam method CreateXlsx untuk mengisi data pada worksheet.
1: /// <summary>
2: /// Mengisi data pada sebuah file excel
3: /// </summary>
4: /// <param name="excel">The excel.</param>
5: private static void FillData(ExcelPackage excel)
6: { 7: int[] jerseyNumber = { 1, 3, 23, 5, 2, 22, 19, 15, 10, 17, 9 }; 8: string[] player = { "Markus", "Zulkifli", "Hamka", "Maman", "Nasuha", "Ridwan", 9: "Bustomi", "Firman", "Okto", "Irfan", "Gonzales" };
10: ExcelWorksheet sheet = excel.Workbook.Worksheets.Add("Indonesia"); 11:
12: sheet.Column(1).Width = 3;
13:
14: sheet.Cell(1, 1).Value = "No.";
15: sheet.Cell(1, 2).Value = "Pemain";
16:
17: for (int i = 0; i < jerseyNumber.Length; i++)
18: { 19: ExcelCell cellJersey = sheet.Cell(i + 2, 1);
20: cellJersey.Value = jerseyNumber[i].ToString();
21:
22: ExcelCell cellPlayer = sheet.Cell(i + 2, 2);
23: cellPlayer.Value = player[i];
24: }
25: }
- Definisikan method SetDocumentProperties yang dipanggil di dalam method CreateXlsx untuk menentukan properti dari dokumen.
1: /// <summary>
2: /// Men-set document properties.
3: /// </summary>
4: /// <param name="excel">The excel.</param>
5: private static void SetDocumentProperties(ExcelPackage excel)
6: { 7: excel.Workbook.Properties.Title = "AFF Cup";
8: excel.Workbook.Properties.Author = "Jeffrey Hermanto Halimsetiawan";
9: excel.Workbook.Properties.Company = "Microsoft Student Partners";
10: }
- Panggil method CreateXlsx pada main.
1: static void Main(string[] args)
2: { 3: CreateXlsx();
4: }
Berikut adalah hasil dari file Excel yang dibuat beserta document properties yang juga telah ter-update:
Selamat mencoba.