Quantcast
Channel: NPOI
Viewing all articles
Browse latest Browse all 1621

Commented Unassigned: .xlsx 如果存在的工作表包含圖面,使用 XSSFWorkbook 開啟後再加入新工作表有問題 [13104]

$
0
0
FileStream T_excelfile = File.Open(this.textBox1.Text,FileMode.Open,FileAccess.Read); //new FileStream(this.textBox1.Text, FileMode.Open,FileAccess.ReadWrite);
IWorkbook T_workbook = WorkbookFactory.Create(T_excelfile,ImportOption.All); // 依輸入 filetream 自動判斷使用 2003 HSSF(.xls) or 2007 XSSF(.xlsx)
XSSFWorkbook T_workbook = new XSSFWorkbook(T_excelfile);

if (T_workbook.GetSheet("訂單標籤數量") != null)
if (MessageBox.Show("標籤檔已存在該張訂單標籤數量資料,是否刪除重新轉入?", "資料確認", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
{
return;
}
else
{
T_workbook.SetSheetOrder("訂單標籤數量", 0);
T_workbook.RemoveSheetAt(0);
}

ISheet T_sheet = T_workbook.CreateSheet("訂單標籤數量");

#region 設置匯出EXCEL的文字格式

//設置字體
IFont T_font = T_workbook.CreateFont();
//字體名稱
T_font.FontName = "新細明體";
//設置字體大小
T_font.FontHeightInPoints = 12;
//設置字串列的樣式
ICellStyle T_style = T_workbook.CreateCellStyle();
//設置字體顯示樣式
T_style.SetFont(T_font);

#endregion

#region 寫入 工作表欄位標題
// 產生 Excel 標題列
IRow T_row = T_sheet.CreateRow(0);

ICell T_cell =T_row.CreateCell(0); // 產生第一個欄位(訂單明細序號)

T_cell.CellStyle = T_style;
T_cell.SetCellValue(this.dataGridView1.Columns["TD003"].HeaderText);

T_cell = T_row.CreateCell(1); // 產生第二個欄位(訂單明細品號)
T_cell.CellStyle = T_style;
T_cell.SetCellValue(this.dataGridView1.Columns["TD004"].HeaderText);

T_cell = T_row.CreateCell(2); // 產生第三個欄位(訂單數量)
T_cell.CellStyle = T_style;
T_cell.SetCellValue(this.dataGridView1.Columns["TD008"].HeaderText);

for (int i = 0; i < this.dataGridView2.ColumnCount; i++) // 產生第4,5,6個欄位(小包、大包、外箱)
{
if (this.dataGridView2.Columns[i].Visible == true)
{
T_cell = T_row.CreateCell(3 + i);
T_cell.CellStyle = T_style;
T_cell.SetCellValue(this.dataGridView2.Columns[i].HeaderText);
}
}
T_excelfile = new FileStream(this.textBox1.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite);
T_workbook.Write(T_excelfile);

T_workbook = null;
T_excelfile.Close();
T_excelfile.Dispose();

只是在現有工作表再新增一個工作表再存檔,便會出現 「內容有問題,嘗試復原」的錯誤訊息!!
Comments: ** Comment from web user: peterwu57 **

如附件。


Viewing all articles
Browse latest Browse all 1621

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>