返回目录:excel表格制作
public SXSSFWorkbook(XSSFWorkbook workbook)
Construct a workbook from a template.
There are three use-cases to use
SXSSFWorkbook(XSSFWorkbook) :Append new sheets to existing workbooks. You can open existing workbook from
a file or create on the fly with XSSF.Append rows to existing sheets. The row number MUST be greater than
max(rownum) in the template sheet.Use existing workbook as a template and re-use global objects such as cell
styles, formats, images, etc.Access initial cells and rows in the template. After constructing SXSSFWorkbook(XSSFWorkbook) all internal windows are empty and SXSSFSheet.getRow(int) and SXSSFRow.getCell(int) return null.
Override existing cells and rows. The API silently allows that but the
output file is invalid and Excel cannot read it.
All three use cases can work in a
combination. What is not supported:
API文档e79fa5e98193e78988e69d83333里面有,反正就是说,使用SXSSFWorkbook附加数据到已经存在的Excel中的话就是不行的;SXSSFSheet.getRow(int) 和 SXSSFRow.getCell(int) 会返回空。
SXSSFWorkbook只能用在新创建Excel中才行。
这说明你读表的表路径出了问题,虽然是同一张表,但是你把表复制到其他地方再进行修改当然不会更新
修改完需636f7079e799bee5baa6e79fa5e98193333要写入,也就是保存一下的。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ChangeCell {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
String fileToBeRead = "C:\\exp.xls"; // excel位置
int coloum = 1; // 比如你要获取第1列
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
fileToBeRead));
HSSFSheet sheet = workbook.getSheet("Sheet1");
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
HSSFRow row = sheet.getRow((short) i);
if (null == row) {
continue;
} else {
HSSFCell cell = row.getCell((short) coloum);
if (null == cell) {
continue;
} else {
System.out.println(cell.getNumericCellValue());
int temp = (int) cell.getNumericCellValue();
cell.setCellValue(temp + 1);
}
}
}
FileOutputStream out = null;
try {
out = new FileOutputStream(fileToBeRead);
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}