Npoi 2.0 tutorial (II): Edit Existing Excel files

Source: Internet
Author: User
Npoi 2.0 tutorial (II): Edit Existing Excel file categories: C # technology 993 people read comments (3) collect reports C # excelnpoi

Reprinted please indicate the source http://blog.csdn.net/fujie724

The previous article describes how to use npoi to create a new Excel file, but sometimes we need to edit and modify an existing Excel file. So what should I do with npoi?

Continue to learn

In this article, we read the Excel file generated in the previous tutorial, use it as a template, modify it, and save it as another Excel file.

 

Step 1

 

[CSHARP]View plaincopyprint?
  1. // The file generated in the previous tutorial
  2. String temppath = "d :\\ excel.xls ";
  3. Hssfworkbook wk = NULL;
  4. Using (filestream FS = file. Open (temppath, filemode. Open,
  5. Fileaccess. Read, fileshare. readwrite ))
  6. {
  7. // Read the xls file into the workbook variable, and then close it.
  8. Wk = new hssfworkbook (FS );
  9. FS. Close ();
  10. }
// The file string temppath = "D: \ excel.xls" generated in the previous tutorial; hssfworkbook wk = NULL; using (filestream FS = file. open (temppath, filemode. open, fileaccess. read, fileshare. readwrite) {// read the xls file into the workbook variable, and then you can disable wk = new hssfworkbook (FS); FS. close ();}

 

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:

I want to use the cell in the first column of the Second row. This position is not created in the previous tutorial, so it must be created first.

 

[CSHARP]View plaincopyprint?
  1. // Create a row in the second row
  2. Irow ROW = sheet. createrow (1 );
  3. // Create a cell in the first column of the Second row
  4. Icell cell = row. createcell (0 );
// 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.

 

In the previous tutorial, we have created cells and assigned values for the position in the first column of the first row of the file. Therefore, you can directly use them using the following method.

 

[CSHARP]View plaincopyprint?
  1. // Obtain the first line
  2. Irow ROW = sheet. getrow (0 );
  3. // Obtain the first cell column in the first row
  4. Icell cell = row. getcell (0 );
// 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:

 

[CSHARP]View plaincopyprint?
  1. // Create the second row and the first column
  2. Icell cell = sheet. createrow (1). createcell (0 );
  3. // Obtain the first row and the first column
  4. Icell cell = sheet. getrow (0). getcell (0 );
// 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

 

[CSHARP]View plaincopyprint?
  1. Sheet. getrow (0). getcell (0). setcellvalue ("edited value ");
Sheet. getrow (0). getcell (0). setcellvalue ("edited value ");

 

Step 3

Save after editing

 

[CSHARP]View plaincopyprint?
  1. Using (filestream = file. Open ("D: \ excel.xls ",
  2. Filemode. openorcreate, fileaccess. readwrite ))
  3. {
  4. WK. Write (filestream );
  5. Filestream. Close ();
  6. }
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.

Npoi 2.0 tutorial (II): Edit Existing Excel 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.