-
StackOverflow 文件
-
epplus 教程
-
儲存 Excel 文件
-
使用 SaveFileDialog 儲存到磁碟
//Using File.WriteAllBytes
using (ExcelPackage excelPackage = new ExcelPackage())
{
//create a new Worksheet
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
//add some text to cell A1
worksheet.Cells["A1"].Value = "My fourth EPPlus spreadsheet!";
//convert the excel package to a byte array
byte[] bin = excelPackage.GetAsByteArray();
//create a SaveFileDialog instance with some properties
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "Save Excel sheet";
saveFileDialog1.Filter = "Excel files|*.xlsx|All files|*.*";
saveFileDialog1.FileName = "ExcelSheet_" + DateTime.Now.ToString("dd-MM-yyyy") + ".xlsx";
//check if user clicked the save button
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//write the file to the disk
File.WriteAllBytes(saveFileDialog1.FileName, bin);
}
}
//Using SaveAs
using (ExcelPackage excelPackage = new ExcelPackage())
{
//create a new Worksheet
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
//add some text to cell A1
worksheet.Cells["A1"].Value = "My fourth EPPlus spreadsheet!";
//create a SaveFileDialog instance with some properties
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "Save Excel sheet";
saveFileDialog1.Filter = "Excel files|*.xlsx|All files|*.*";
saveFileDialog1.FileName = "ExcelSheet_" + DateTime.Now.ToString("dd-MM-yyyy") + ".xlsx";
//check if user clicked the save button
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//Get the FileInfo
FileInfo fi = new FileInfo(saveFileDialog1.FileName);
//write the file to the disk
excelPackage.SaveAs(fi);
}
}