Upgrading to POI 3.5, including converting existing HSSF Usermodel code to SS Usermodel (for XSSF and HSSF)(升级到 POI 3.5,包括将现有 HSSF Usermodel代码转换为 SS Usermodel(用于 XSSF 和 HSSF))

Things that have to be changed when upgrading to POI 3.5(升级到 POI 3.5 时必须更改的内容)

Wherever possible, we have tried to ensure that you can use your existing POI code with POI 3.5 without requiring any changes. However, Java doesn't always make that easy, and unfortunately there are a few changes that may be required for some users.(我们尽可能确保您可以在POI 3.5中使用现有的POI代码,而无需做任何更改。但是,在Java没这么简单,所以,有些靓仔可能需要对程序做一些更改。)

org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue)

Annoyingly, java will not let you access a static inner class via a child of the parent one. So, all references to org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue will need to be changed to org.apache.poi.ss.usermodel.FormulaEvaluator.CellValue(恼火的是,java 不允许您通过父类的子类访问静态内部类。因此,所有对 org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue 的引用都需要更改为 org.apache.poi.ss.usermodel.FormulaEvaluator.CellValue)

org.apache.poi.hssf.usermodel.HSSFRow.MissingCellPolicy(org.apache.poi.hssf.usermodel.HSSFRow.MissingCellPolicy)

Annoyingly, java will not let you access a static inner class via a child of the parent one. So, all references to org.apache.poi.hssf.usermodel.HSSFRow.MissingCellPolicy will need to be changed to org.apache.poi.ss.usermodel.Row.MissingCellPolicy(恼火的是,java 不允许您通过父类的子类访问静态内部类。因此,所有对 org.apache.poi.hssf.usermodel.HSSFRow.MissingCellPolicy 的引用都需要更改为 org.apache.poi.ss.usermodel.Row.MissingCellPolicy)

DDF and org.apache.poi.hssf.record.RecordFormatException(DDF 和 org.apache.poi.hssf.record.RecordFormatException)

Previously, record level errors within DDF would throw an exception from the hssf class hierarchy. Now, record level errors within DDF will throw a more general RecordFormatException, org.apache.poi.util.RecordFormatException(以前,DDF 中的记录级错误会引发hssf类层次结构中的异常。现在,DDF 中的记录级别错误将引发更一般的 RecordFormatException,org.apache.poi.util.RecordFormatException)

In addition, org.apache.poi.hssf.record.RecordFormatException has been changed to inherit from the new org.apache.poi.util.RecordFormatException, so you may wish to change catches of the hssf version to the new util version.(此外,org.apache.poi.hssf.record.RecordFormatException 已更改为从新的 org.apache.poi.util.RecordFormatException 继承,因此您可能需要将 hssf 版本的异常捕获更改为新的 util 版本。)

Converting existing HSSF Usermodel code to SS Usermodel (for XSSF and HSSF)(将现有 HSSF Usermodel代码转换为 SS Usermodel(用于 XSSF 和 HSSF))

Why change?(为什么要改?)

If you have existing HSSF usermodel code that works just fine, and you don't want to use the new OOXML XSSF support, then you probably don't need to. Your existing HSSF only code will continue to work just fine.(如果您现有的HSSF 用户模型代码可以正常工作,并且您不想使用新的OOXML XSSF支持,那么您可能不需要改。您现有的代码仅 HSSF 部分代码将继续正常工作。)

However, if you want to be able to work with both HSSF for your .xls files, and also XSSF for .xslx files, then you will need to make some slight tweaks to your code.(但是,如果您希望能够同时使用 .xls 文件的 HSSF 和 .xslx 文件的 XSSF,那么您需要对您的代码进行一些细微的调整。)

org.apache.poi.ss.usermodel(org.apache.poi.ss.usermodel)

The new SS usermodel (org.apache.poi.ss.usermodel) is very heavily based on the old HSSF usermodel (org.apache.poi.hssf.usermodel). The main difference is that the package name and class names have been tweaked to remove HSSF from them. Otherwise, the new SS Usermodel interfaces should provide the same functionality.(新的 SS usermodel (org.apache.poi.ss.usermodel) 在很大程度上基于旧的 HSSF 用户模型 (org.apache.poi.hssf.usermodel)。主要区别在于对软件包名称和类名称进行了调整,以从中删除HSSF。新的 SS Usermodel 接口会提供与HSSF相同的功能。)

Constructors(构造函数)

Calling the empty HSSFWorkbook remains as the way to create a new, empty Workbook object. To open an existing Workbook, you should now call WorkbookFactory.create(inp).(调用空的 HSSFWorkbook 仍然是创建新的空 Workbook 对象的方法。要打开现有工作簿,您现在应该调用 WorkbookFactory.create(inp)。)

For all other cases when you would have called a Usermodel constructor, such as 'new HSSFRichTextString()' or 'new HSSFDataFormat', you should instead use a CreationHelper. There's a method on the Workbook to get a CreationHelper, and the CreationHelper will then handle constructing new objects for you.(对于调用 Usermodel 构造函数的所有其他情况,例如“new HSSFRichTextString()”或“new HSSFDataFormat”,您应该改用 CreationHelper。 Workbook 上有一个获取 CreationHelper 的方法,CreationHelper 将为您构建新对象。)

Other Code(其他代码)

For all other code, generally change a reference from org.apache.poi.hssf.usermodel.HSSFFoo to a reference to org.apache.poi.ss.usermodel.Foo. Method signatures should otherwise remain the same, and it should all then work for both XSSF and HSSF.(对于所有的其他代码,通常将引用从 org.apache.poi.hssf.usermodel.HSSFFoo 更改为 org.apache.poi.ss.usermodel.Foo。否则,方法签名应当保持不变,并且它应当同时适用于XSSF和HSSF。)

Worked Examples(工作示例)

Old HSSF Code(旧 HSSF 代码)

// import org.apache.poi.hssf.usermodel.*;
HSSFWorkbook wb = new HSSFWorkbook();
// create a new sheet
// 创建一个新工作表
HSSFSheet s = wb.createSheet();
// declare a row object reference
// 声明一个行对象引用
HSSFRow r = null;
// declare a cell object reference
// 声明一个单元格对象引用
HSSFCell c = null;
// create 2 cell styles
// 创建 2 个单元格样式
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFDataFormat df = wb.createDataFormat();
// create 2 fonts objects
// 创建 2 个字体对象
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
// Set font 1 to 12 point type, blue and bold
// 设置字体 1 为 12 点类型,蓝色和粗体
f.setFontHeightInPoints((short) 12);
f.setColor( HSSFColor.RED.index );
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// Set font 2 to 10 point type, red and bold
// 将字体 2 设置为 10 点类型,红色和粗体
f2.setFontHeightInPoints((short) 10);
f2.setColor( HSSFFont.RED.index );
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// Set cell style and formatting
// 设置单元格样式和格式
cs.setFont(f);
cs.setDataFormat(df.getFormat("#,##0.0"));
// Set the other cell style and formatting
// 设置其他单元格样式和格式
cs2.setBorderBottom(cs2.BORDER_THIN);
cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
cs2.setFont(f2);
// Define a few rows
// 定义一些行
for(short rownum = (short)0; rownum < 30; rownum++) {
HSSFRow r = s.createRow(rownum);
for(short cellnum = (short)0; cellnum < 10; cellnum += 2) {
HSSFCell c = r.createCell(cellnum);
HSSFCell c2 = r.createCell(cellnum+1);
c.setCellValue((double)rownum + (cellnum/10));
c2.setCellValue(new HSSFRichTextString("Hello! " + cellnum);
}
}
// Save
// 保存
FileOutputStream out = new FileOutputStream("workbook.xls");
wb.write(out);
out.close();

New, generic SS Usermodel Code(新的通用 SS Usermodel 代码)

// import org.apache.poi.ss.usermodel.*;
Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() };
for(int i=0; i<wbs.length; i++) {
Workbook wb = wbs[i];
CreationHelper createHelper = wb.getCreationHelper();
// create a new sheet
// 创建一个新工作表
Sheet s = wb.createSheet();
// declare a row object reference
// 声明一个行对象引用
Row r = null;
// declare a cell object reference
// 声明一个单元格对象引用
Cell c = null;
// create 2 cell styles
// 创建 2 个单元格样式
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
DataFormat df = wb.createDataFormat();
// create 2 fonts objects
// 创建 2 个字体对象
Font f = wb.createFont();
Font f2 = wb.createFont();
// Set font 1 to 12 point type, blue and bold
// 设置字体 1 为 12 点类型,蓝色和粗体
f.setFontHeightInPoints((short) 12);
f.setColor( IndexedColors.RED.getIndex() );
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
// Set font 2 to 10 point type, red and bold
// 将字体 2 设置为 10 点类型,红色和粗体
f2.setFontHeightInPoints((short) 10);
f2.setColor( IndexedColors.RED.getIndex() );
f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
// Set cell style and formatting
// 设置单元格样式和格式
cs.setFont(f);
cs.setDataFormat(df.getFormat("#,##0.0"));
// Set the other cell style and formatting
// 设置其他单元格样式和格式
cs2.setBorderBottom(cs2.BORDER_THIN);
cs2.setDataFormat(df.getFormat("text"));
cs2.setFont(f2);
// Define a few rows
// 定义一些行
for(int rownum = 0; rownum < 30; rownum++) {
Row r = s.createRow(rownum);
for(int cellnum = 0; cellnum < 10; cellnum += 2) {
Cell c = r.createCell(cellnum);
Cell c2 = r.createCell(cellnum+1);
c.setCellValue((double)rownum + (cellnum/10));
c2.setCellValue(
createHelper.createRichTextString("Hello! " + cellnum)
);
}
}
// Save
// 保存
String filename = "workbook.xls";
if(wb instanceof XSSFWorkbook) {
filename = filename + "x";
}
FileOutputStream out = new FileOutputStream(filename);
wb.write(out);
out.close();
}

by Nick Burch(作者:尼克伯奇)

 
中英文 | 中文 | 英文