C # Methods for generating Excel files with exact column widths and high row heights based on Npoi

Source: Internet
Author: User
Tags sqlite database

The example of this paper describes the method of C # based on Npoi to generate Excel files with exact column widths, and is a very useful technique to share with everyone for your reference. The method is as follows:.


First, preface

Npoi is the artifact that operates Excel, export import fast as lightning,

But the Setcolumnwidth function personal feeling will not be used, how can not control, because he is to set the number of characters to the width, in fact, Excel column widths have a pixel concept, more difficult to understand.

Usage and annotations with setcolumnwidth iworkbook hssfworkbook = new Hssfworkbook (); Isheet Sheet1 = Hssfworkbook. Createsheet ("Sheet1"); Sheet1. Setcolumnwidth (1, 100 * 256);//Abstract://   Set the width (in units of 1/256th of a character width) the maximum Column//
   width for a individual cell is 255 characters. This value represents the//number of   characters so can be displayed in a cell that's formatted with//the   Stan Dard font.////parameter://  columnindex://the   column to set (0-based)////  width://the width in units of   1/256t H of a character width

The notes are in English, but they are easier to understand.


Second, the problems encountered

So if you need to generate a fixed format of Excel, to have a precise column width and row height, to be used for printing and other special needs, how to solve it


Third, the solution

Set up a row of high column width of the XLS as a template, where the need to fill the character of a random dot character (to avoid Npoi task the cell is null), set the font and so on, using Npoi open the XLS file, the corresponding space SetValue, and then save the file, Because Npoi is for the operation of the file stream instead of using Excel.exe to operate Excel, the resulting file is very efficient, two seconds less than can generate 100 content in the same format as the cell style Excel file, Npoi is worthy of operation of the XLS artifact, this can save a lot of effort, even the font size character Center Still on the right, color can be set in the template without the Npoi settings, after all, write code to set the cell style feeling is a very troublesome thing


Iv. concluding remarks

This solution is only suitable for situations where you need to output a small number of characters, for cases where the output character count is indeterminate, because it may fill the cell causing the column width or the row height to change, thus affecting the print preview, etc., which is not described because it is not tested.

It is believed that this article has some reference value to the study of C # program design.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Methods for generating Excel files with exact column widths and high row heights based on Npoi

This address: http://www.paobuke.com/develop/c-develop/pbk23480.html






Related content ASP (C #) Programming An example of how to turn a color picture into a grayscale image the type system (value type and reference type) in C # manipulate the reading and writing database of SQLite database C # algorithm function: Gets the maximum length of a number in a string
C # Implementation of methods to start, close and find a process C # Implementing a method of emptying the Recycle Bin C # How to implement a simple chat program for multithreaded, multi-task management (demo) C #

C # Methods for generating Excel files with exact column widths and high row heights based on Npoi

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.