Preface
Npoi is an Excel operating tool. exporting and importing data is as fast as lightning,
However, the setcolumnwidth function is not applicable to anyone. It cannot be controlled because it is used to set the width based on the number of characters. In fact, there is still a pixel concept in the Excel column width, so I cannot understand it.
// Use and comment with setcolumnwidth
[CSHARP]View plaincopy
- Iworkbook hssfworkbook = new hssfworkbook ();
- Isheet sheet1 = hssfworkbook. createsheet ("sheet1 ");
- Sheet1.setcolumnwidth (1,100*256 );
- // Summary:
- // Set the width (in units of 1/256th of a character width) the maximum Column
- // Width for an individual cell is 255 characters. This value represents
- // Number of characters that can be displayed in a cell that is formatted
- // The Standard font.
- //
- // Parameters:
- // Columnindex:
- // The column to set (0-based)
- //
- // Width:
- // The width in units of 1/256th of a character width
Looking at this comment, I think it hurts.
Problems encountered
If you want to generate an Excel file in a fixed format, you must have the exact column width and row height, and use it for printing and other special requirements. How can this problem be solved?
Solution
Haha, opportunistic approach,
Create an xls template that has configured the Row Height and column width as the template. Fill in a few characters wherever you want to fill the characters (to avoid the npoi task, the cell is null) and set the font and other content,
Sheet to Operate Excel, so the efficiency of file generation is extremely high,
In less than two seconds, you can generate one hundred Excel files with different cell styles. npoi is worthy of being an xls operating tool,
This saves a lot of effort. Even if the font size is centered or right aligned, the color can be set in the template without the need to use npoi settings, after all, it is quite troublesome to write code to set cell styles.
Conclusion
This solution is only suitable for the case where a small number of characters need to be output, and the number of output characters is uncertain, because the column width or Row Height may change due to the filling of cells,
This affects print preview and so on. In this case, we will not describe it because it is not tested.
C # Use npoi to generate an Excel file with exact wide columns and rows