apache.org提供了一个项目(poi)专门用于对办公软件的操作。这样大大方便了我们操作和读取其中的内容,用于统计分析。我下载的版本为poi-bin-3.6-20091214.zip
Component APIs
Excel (SS=HSSF+XSSF)
Word (HWPF+XWPF)
PowerPoint (HSLF+XSLF)
OpenXML4J (OOXML)
OLE2 Filesystem (POIFS)
OLE2 Document Props (HPSF)
Outlook (HSMF)
Visio (HDGF)
Publisher (HPBF)
在文档中提供了2种不同的写入方法,一个是老代码,一个是新代码。如果你希望能够兼容xls文件和xslx文件,那么建议用新代码。且老代码对office2007的操作不是很好。
代码如下:
// import org.apache.poi.hssf.usermodel.*;
public static void main(String[] args)throws Exception {
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( HSSFColor.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++) {
r = s.createRow(rownum);
for(short cellnum = (short)0; cellnum < 10; cellnum += 2) {
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("d://workbook.xls");
wb.write(out);
out.close();
}
// import org.apache.poi.hssf.usermodel.*;
public static void main(String[] args) throws Exception {
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++) {
r = s.createRow(rownum);
for (int cellnum = 0; cellnum < 10; cellnum += 2) {
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 = "d://workbook.xls";
if (wb instanceof XSSFWorkbook) {
filename = filename + "x";
}
FileOutputStream out = new FileOutputStream(filename);
wb.write(out);
out.close();
}
}