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.