NPOI2.0 tutorial (II): Edit Existing EXCEL files

Source: Internet
Author: User
After reading step 2, you can operate the HSSFWorkbook class in the same way as in the first tutorial. One rule that must be followed is that if you want to edit rows and units that are not or have never been created, you must first create them. For example, create row IRowrowsheet. CreateRow (1) in the second row.

After reading step 2, you can operate the HSSFWorkbook class in the same way as in the first tutorial. One rule that must be followed is that if you want to edit rows and units that are not or have never been created, you must first create them. For example: // create the row IRow row = sheet. CreateRow (1) in the second row; // In the second row


Continue to learn


Step 1

 


Step 2

After reading, the operation of the HSSFWorkbook class is the same as that in the first tutorial.

One rule that must be followed is that if you want to edit rows and cells that have no value or have never been created, you must first create them.

For example:

// Create the row IRow row = sheet. CreateRow (1) in the second row; // create the cell ICell cell = row. CreateCell (0) in the first column of the Second row );

Then the cell can be used.

  • What if I don't create one?
  • A: The returned value of sheet. GetRow (1) is null. If you use GetCell (0) to return null, an error of the empty object is returned.
// Obtain the first row IRow row = sheet. GetRow (0); // obtain the first cell ICell cell = row. GetCell (0) in the first row );
The preceding two methods can also be abbreviated as follows:
// Create the second row. The first column is ICell cell = sheet. createRow (1 ). createCell (0); // obtain the first row, the first column ICell cell = sheet. getRow (0 ). getCell (0 );

Then, the cell. SetCellValue () function is used to assign values to it to complete editing.


Of course, the value assignment statement can also be written in one line with the preceding statement. For example

Sheet. GetRow (0). GetCell (0). SetCellValue ("edited value ");


Step 3

Save after editing

using (FileStream fileStream = File.Open("d:\\excel.xls",FileMode.OpenOrCreate, FileAccess.ReadWrite)){    wk.Write(fileStream);    fileStream.Close();}

This completes reading, editing, and saving existing files.

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.