C # Use npoi to generate an Excel file with exact wide columns and rows

Source: Internet
Author: User

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
  1. Iworkbook hssfworkbook = new hssfworkbook ();
  2. Isheet sheet1 = hssfworkbook. createsheet ("sheet1 ");
  3. Sheet1.setcolumnwidth (1,100*256 );
  4. // Summary:
  5. // Set the width (in units of 1/256th of a character width) the maximum Column
  6. // Width for an individual cell is 255 characters. This value represents
  7. // Number of characters that can be displayed in a cell that is formatted
  8. // The Standard font.
  9. //
  10. // Parameters:
  11. // Columnindex:
  12. // The column to set (0-based)
  13. //
  14. // Width:
  15. // 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.