private void genSimpleExcel()
{
System.IO.FileInfo filePath = new System.IO.FileInfo(@"C:\Users\MarkShu\Desktop\test1.xlsx");
if (System.IO.File.Exists(filePath.ToString()))
{
System.IO.File.Delete(filePath.ToString());
}
if (!System.IO.File.Exists(filePath.ToString()))
{
ExcelPackage ep = new ExcelPackage(filePath);
ExcelWorksheet ws = ep.Workbook.Worksheets.Add("Sheet1");
ws.Cells[1, 1 , 1 , 3].Value = "Name";
ws.Cells[1, 1, 1, 3].Merge = true;
ws.Cells[1, 1, 1, 3].Style.Font.Size = 18;
ws.Cells[1, 1, 1, 3].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
//ws.Cells[1, 2].Value = "score";
ws.Cells["B2:B4"].Style.Numberformat.Format = "0.0"; //0.0 #自動捨五入
ws.Cells["C4"].Formula = "SUM(B2:B4)";
ws.Cells[2, 1].Value = "abey";
ws.Cells[2, 1].Style.Font.Color.SetColor(Color.Green);
ws.Cells[2, 1].Style.Font.Bold = true;
ws.Cells[3, 1].Value = "2018/9/15";
ws.Cells[3, 1].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
ws.Cells[3, 1].Style.Border.Bottom.Color.SetColor(Color.Red);
ws.Cells[4, 1].Value = "18:35:51";
ws.Cells[2, 2].Value = 85.5;
ws.Cells[2, 2].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
ws.Cells[3, 2].Value = 82.0;
ws.Cells[4, 2].Value = Convert.ToDouble("84");
ws.Cells[4, 2].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
ws.Cells[4, 2].Style.Fill.BackgroundColor.SetColor(Color.Yellow); //Can't set color when patterntype is not set.
List<string[]> dataRow = new List<string[]>()
{
new string[] { "標題1", "標題2", "標題3" } //資料
};
dataRow.Add(new string[] { "TAIPEI", "TOKYOK", "NEWYORK" });
//Cells[] = ExcelRange
//ExcelRange range = ws.SelectedRange[7, 1, 8, 3]; //位置
ExcelRange range = ws.Cells[7, 1, 8, 3];
range.LoadFromArrays(dataRow); //將資料放入位置
range.AutoFitColumns();
ws.Column(1).AutoFit(); //start from 1
ws.Cells[ws.Dimension.Address].AutoFitColumns();
ep.Save();
}
}
{
System.IO.FileInfo filePath = new System.IO.FileInfo(@"C:\Users\MarkShu\Desktop\test1.xlsx");
if (System.IO.File.Exists(filePath.ToString()))
{
System.IO.File.Delete(filePath.ToString());
}
if (!System.IO.File.Exists(filePath.ToString()))
{
ExcelPackage ep = new ExcelPackage(filePath);
ExcelWorksheet ws = ep.Workbook.Worksheets.Add("Sheet1");
ws.Cells[1, 1 , 1 , 3].Value = "Name";
ws.Cells[1, 1, 1, 3].Merge = true;
ws.Cells[1, 1, 1, 3].Style.Font.Size = 18;
ws.Cells[1, 1, 1, 3].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
//ws.Cells[1, 2].Value = "score";
ws.Cells["B2:B4"].Style.Numberformat.Format = "0.0"; //0.0 #自動捨五入
ws.Cells["C4"].Formula = "SUM(B2:B4)";
ws.Cells[2, 1].Value = "abey";
ws.Cells[2, 1].Style.Font.Color.SetColor(Color.Green);
ws.Cells[2, 1].Style.Font.Bold = true;
ws.Cells[3, 1].Value = "2018/9/15";
ws.Cells[3, 1].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick;
ws.Cells[3, 1].Style.Border.Bottom.Color.SetColor(Color.Red);
ws.Cells[4, 1].Value = "18:35:51";
ws.Cells[2, 2].Value = 85.5;
ws.Cells[2, 2].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
ws.Cells[3, 2].Value = 82.0;
ws.Cells[4, 2].Value = Convert.ToDouble("84");
ws.Cells[4, 2].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
ws.Cells[4, 2].Style.Fill.BackgroundColor.SetColor(Color.Yellow); //Can't set color when patterntype is not set.
List<string[]> dataRow = new List<string[]>()
{
new string[] { "標題1", "標題2", "標題3" } //資料
};
dataRow.Add(new string[] { "TAIPEI", "TOKYOK", "NEWYORK" });
//Cells[] = ExcelRange
//ExcelRange range = ws.SelectedRange[7, 1, 8, 3]; //位置
ExcelRange range = ws.Cells[7, 1, 8, 3];
range.LoadFromArrays(dataRow); //將資料放入位置
range.AutoFitColumns();
ws.Column(1).AutoFit(); //start from 1
ws.Cells[ws.Dimension.Address].AutoFitColumns();
ep.Save();
}
}