Home > Net >  C # NPIO write EXCEL tip error every time you open the EXCEL.
C # NPIO write EXCEL tip error every time you open the EXCEL.

Time:09-15

Error as follows, vexed ace to give directions

CodePudding user response:

Look not to come out, have the time I can see the npio read before I write, write several excel blog
Excel reading: https://blog.csdn.net/yysyangyangyangshan/article/details/42614209
Excel to: https://blog.csdn.net/yysyangyangyangshan/article/details/42837841
As well as in a web page to keep write excel and prompt the user to specify path:
https://blog.csdn.net/yysyangyangyangshan/article/details/47904119

CodePudding user response:

reference 1st floor Yang Youshan response:
look not to come out, have the time I can see the npio read before I write, write several excel blog
Excel reading: https://blog.csdn.net/yysyangyangyangshan/article/details/42614209
Excel to: https://blog.csdn.net/yysyangyangyangshan/article/details/42837841
As well as in a web page to keep write excel and prompt the user to specify path:
https://blog.csdn.net/yysyangyangyangshan/article/details/47904119

Thank you, your post is to use the XLS. I XLSX, it may not seem like a type of file

CodePudding user response:

Read the XLS and XLSX is to use two different work

CodePudding user response:

My code
The class dt2excel
{

Public static void DataTableToExcel (XSSFWorkbook workbook, XSSFSheet sheet,
The DataTable dt, string strFileName, int startline, List The columns, List Columnsfixed)
{
Using (var fileStream=new fileStream (strFileName, FileMode. OpenOrCreate))
{
///formatting options
ICellStyle cellStyle=workbook. CreateCellStyle ();
IFont font=workbook. CreateFont ();
The font. FontName="tahoma";
The font. FontHeightInPoints=12;
CellStyle. DataFormat=194;
CellStyle. SetFont (font);
CellStyle. Alignment=NPOI. SS. UserModel. HorizontalAlignment. Center;
CellStyle. VerticalAlignment=VerticalAlignment. Center;
CellStyle. BorderBottom=NPOI. SS. UserModel. BorderStyle. Thin;
CellStyle. BorderLeft=NPOI. SS. UserModel. BorderStyle. Thin;
CellStyle. BorderRight=NPOI. SS. UserModel. BorderStyle. Thin;
CellStyle. BorderTop=NPOI. SS. UserModel. BorderStyle. Thin;
CellStyle. WrapText=true;



Sheet. GetRow (1). CreateCell (7). The CellStyle=CellStyle;
Sheet. GetRow (1). GetCell (7). SetCellValue (DateTime. Now. The ToString (" yyyy/MM/dd "));//title
Sheet. GetRow (2). CreateCell (7). The CellStyle=CellStyle;
Sheet. GetRow (2). GetCell (7). SetCellValue (dt) Rows) Count);



If (dt!=null & amp; & Dt. Rows. Count & gt; 0)
{

For (int I=0; I & lt; Dt. Rows. Count; I++)
{
IRow newRow=sheet. CreateRow (I + 1 + startline);//row from line 1 to write, not row 0, 0 behavior title.
//IRow newRow=sheet. GetRow (I + 1 + startline);
For (int j=0; J & lt; The columns. The Count; J++)
{
Int columnnr=int. Parse (OperateIniFile ReadIniData (" columns ", the columns [j], ""));
ICell cell=newRow. CreateCell (columnnr - 1);
Cell. SetCellValue (dt) Rows [I] [j]==null? "" : dt. Rows [I] [j]. The ToString ());
//cell. SetCellValue (dt);
//cell. SetCellValue (float. Parse ((dt) Rows [I] [j]). The ToString ()));
Cell. The CellStyle=CellStyle;
}
For (int k=0; K & lt; Columnsfixed. Count; K++)
{
Int columnnr=int. Parse (OperateIniFile ReadIniData (" columnsfixed columnsfixed [k], ""));
ICell cell=newRow. CreateCell (columnnr - 1);
Cell. SetCellValue (columnsfixed [k]);
Cell. The CellStyle=CellStyle;
}
}


//IRow newRow=sheet. CreateRow (I + 1 + startline);//row from line 1 to write, not row 0, 0 behavior title.
////IRow newRow=sheet. GetRow (I + 1 + startline);
//
//}
}

Workbook. Write (fileStream);
Workbook. The Clear ();
Workbook. The Close ();
}
//return filePath.
}

///template is used to derive the Excel
///& lt;/summary>
///& lt; Param name="table" & gt;
///& lt; Param name="strFileName & gt;" The export path & lt;/param>
///& lt; Param name="templetPath & gt;" The template path & lt;/param>
///& lt; Param name="while the startRow" & gt; Which line from start to write the data, starting from 1 & lt;/param>
Public static void ExportExcelByTemple (System. Data. DataTable dtSource,
String strFileName, string templetPath, int, while the startRow List The columns, List Columnsfixed)
{
Try
{
XSSFWorkbook workbook=getWorkBook (templetPath);
XSSFSheet sheet=getSheet (workbook);
String outfile=strFileName;
DataTableToExcel (workbook, sheet, dtSource strFileName, while the startRow, columns, columnsfixed);

//writeData (workbook, sheet, dtSource strFileName, rowHeight, while the startRow);
//saveData (workbook, strFileName);
}
The catch (Exception ex)
{
//LogInfo. Log (ex);
Throw the ex.
}
}
///& lt; summary>
///parse Excel template, return to the WorkBook
///& lt;/summary>
///& lt; Param name="templetPath & gt;"
///& lt; Returns>
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related