Home > Net >  Why NPOI export Excel column widths, set out?
Why NPOI export Excel column widths, set out?

Time:02-23

If (while the startRow & lt; While the startRow=0, 0);//line if less than 0, the default is set to 0
Var wb=new XSSFWorkbook ();//use this object to create workbook variable
Var sheet=wb. CreateSheet (sheetName);//use this object to create a working table variable
//define cell
ICell cell;
Int j;
Var maxLength=0;//maximum number of rows
Int curLenth;//the current line
The object columnValue;//column value
Var dt=dt2. Tables [0];//dt variables, used to store the imported data
Var row=sheet. CreateRow (0);//create a header
Row. HeightInPoints=25;//the height

sheet. SetColumnWidth (0, 10 * 256); Set up a useless//column width


for (int i=0; I & lt; Dt. Columns. The Count; I++)//traverse to import data
{
[I] columnValue=https://bbs.csdn.net/topics/dt.Columns. ColumnName;//set the traverse to the column name
CurLenth=Encoding. The Default. GetByteCount (columnValue. ToString ());//to encode the column name
MaxLength=(maxLength & lt; CurLenth? CurLenth: maxLength);//specifies the maximum length, if the maximum length is less than the current length, then returns the current length, otherwise returns the largest
Var columnWidth maxLength=256 *;//set the column width
Sheet. SetColumnWidth (I, columnWidth);//set the width of the
Cell=row. CreateCell (I);//create a cell for column
Cell. SetCellValue (columnValue. ToString ());//set the contents of a cell, it is read into the name of the column of

}

//fill each line data
For (var I=while the startRow; I & lt; Dt2. Tables [0]. Rows. Count; I++)
{
Var Dr=dt2. Tables [0]. Rows [I];
The row=sheet. CreateRow (I + 1);
For (j=0; J & lt; Dt. Columns. The Count; J++)
{

ColumnValue=https://bbs.csdn.net/topics/dr [j];
CurLenth=Encoding. The Default. GetByteCount (columnValue. ToString ());//to encode the column name
MaxLength=(maxLength & lt; CurLenth? CurLenth: maxLength);//specifies the maximum length, if the maximum length is less than the current length, then returns the current length, otherwise returns the largest
Var columnWidth maxLength=256 *;//set the column width
Sheet. SetColumnWidth (j, columnWidth);//set the width of the
Cell=row. CreateCell (j);//create a cell for column
Cell. SetCellValue (columnValue. ToString ());//set the contents of a cell, it is read into the name of the column of
}

}
//create a data stream object of the specified file name, if not, you can create
Using (var fs=new System. IO. FileStream (saveFileName, System. IO. FileMode. OpenOrCreate, System. IO, FileAccess. Write))
Wb. Write (fs);//use the workbook to write
  •  Tags:  
  • C#
  • Related