Below are my code, I'm stuck with can't append the contents in the excel, When first time load the program, the excel can output normal But at Second time load the program, the excel will crash badly(can't open). I'm using the FileMode.Append, FileAccess.Write, but still can't works Is there have some step I missed it? please help me to solve this issue, thanks a lot!
XSSFWorkbook XSSFworkbook = new XSSFWorkbook(); //建立活頁簿
ISheet sheet = XSSFworkbook.CreateSheet(tbx_Build.Text); //建立sheet
//設定樣式
ICellStyle headerStyle = XSSFworkbook.CreateCellStyle();
IFont headerfont = XSSFworkbook.CreateFont();
headerStyle.Alignment = HorizontalAlignment.Center; //水平置中
headerStyle.VerticalAlignment = VerticalAlignment.Center; //垂直置中
headerfont.FontName = "Segoe UI";
headerfont.FontHeightInPoints = 12;
headerfont.Boldweight = (short)FontBoldWeight.Bold;
headerStyle.SetFont(headerfont);
XSSFCellStyle cs = (XSSFCellStyle)XSSFworkbook.CreateCellStyle();
cs.WrapText = true; // 設定換行
cs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Top;
//新增標題列
IRow headerrow = sheet.CreateRow(0);//建立行
headerrow.HeightInPoints = 20;
headerrow.CreateCell(0).SetCellValue("System_name");
headerrow.CreateCell(1).SetCellValue("Fixture_name");
headerrow.CreateCell(2).SetCellValue("build_ID");
headerrow.CreateCell(3).SetCellValue("start_time");
headerrow.CreateCell(4).SetCellValue("end_time");
headerrow.CreateCell(5).SetCellValue("serial_number");
headerrow.CreateCell(6).SetCellValue("Status");
headerrow.CreateCell(7).SetCellValue("Symptom_label");
headerrow.CreateCell(8).SetCellValue("Repair");
headerrow.CreateCell(9).SetCellValue("Measurement");
headerrow.CreateCell(10).SetCellValue("Board_slot");
headerrow.CreateCell(11).SetCellValue("Error");
headerrow.CreateCell(12).SetCellValue("Version");
for (int i = 0; i < 13; i++)
{
headerrow.GetCell(i).CellStyle = headerStyle; //套用樣式
}
//填入資料
int rowIndex = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow row = sheet.CreateRow(rowIndex);//建立行
row.HeightInPoints = 18;
row.CreateCell(0).SetCellValue(Convert.ToString(dt.Rows[i]["System_name"]));
row.CreateCell(1).SetCellValue(Convert.ToString(dt.Rows[i]["Fixture_name"]));
row.CreateCell(2).SetCellValue(Convert.ToString(dt.Rows[i]["build_ID"]));
row.CreateCell(3).SetCellValue(Convert.ToString(dt.Rows[i]["start_time"]));
row.CreateCell(4).SetCellValue(Convert.ToString(dt.Rows[i]["end_time"]));
row.CreateCell(5).SetCellValue(Convert.ToString(dt.Rows[i]["serial_number"]));
row.CreateCell(6).SetCellValue(Convert.ToString(dt.Rows[i]["Status"]));
row.CreateCell(7).SetCellValue(Convert.ToString(dt.Rows[i]["Symptom_label"]));
row.CreateCell(8).SetCellValue(Convert.ToString(dt.Rows[i]["Repair"]));
row.CreateCell(9).SetCellValue(Convert.ToString(dt.Rows[i]["Measurement"]));
row.CreateCell(10).SetCellValue(Convert.ToString(dt.Rows[i]["Board_slot"]));
row.CreateCell(11).SetCellValue(Convert.ToString(dt.Rows[i]["Error"]));
row.CreateCell(12).SetCellValue(Convert.ToString(dt.Rows[i]["Version"]));
if (dt.Rows[i]["Error"].ToString().Contains("\n"))
{
row.GetCell(12).CellStyle = cs;
row.HeightInPoints = 45;
}
else if (dt.Rows[i]["Repair"].ToString().Contains("\n"))
{
row.GetCell(12).CellStyle = cs;
row.HeightInPoints = 45;
}
sheet.AutoSizeColumn(i); //欄位自動調整大小
rowIndex++;
}
string newName2 = "Yield.xlsx";
string exportpath2 = server_backup_failed + "\\output";
if (!System.IO.Directory.Exists(exportpath2))
{
System.IO.Directory.CreateDirectory(exportpath2);//不存在就建立目錄
}
var file2 = new FileStream(exportpath2 + "\\" + newName2, FileMode.Append, FileAccess.Write);
XSSFworkbook.Write(file2, true);
file2.Close();
XSSFworkbook.Close();
}
FileMode.Append. You'll have to load the file, then add your cells, then overwrite the existing file.FileMode.Append, You can load the excel file in aDataSetor aDataTable. Add the desired data inside that and overwrite the loaded file with new data.