关于Java读取Excel2007的文章在Google、百度上搜索一下,没有太好的例子,实现的也不算太好。查看了一下Poi,最新的 POI 3.5 beta 4 支持读写 Excel2007和PPT2007(XLSX and PPTX),自己来实现Java读取Excel2007了。下载 POI 3.5 beta 4 解压,把其中的jar包导入项目文件。
org.apache.poi对更高excel文档的操作
1.导入jar包
dom4j-20040902.021138.jar
ooxml-schemas-1.1.jar
poi-3.9.jar
poi-ooxml-3.9.jar
xmlbeans-2.5.0.jar
2.案例代码
package xls;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XlsxUtil {
/**
* 写入
* @param path
*/
public void writeExcel2007(String path){
XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Fonts");
Font font0 = wb.createFont();
font0.setColor(IndexedColors.BROWN.getIndex());
CellStyle style0 = wb.createCellStyle();
style0.setFont(font0);
Font font1 = wb.createFont();
font1.setFontHeightInPoints((short)14);
font1.setFontName("Courier New");
font1.setColor(IndexedColors.RED.getIndex());
CellStyle style1 = wb.createCellStyle();
style1.setFont(font1);
Font font2 = wb.createFont();
font2.setFontHeightInPoints((short)16);
font2.setFontName("Arial");
font2.setColor(IndexedColors.GREEN.getIndex());
CellStyle style2 = wb.createCellStyle();
style2.setFont(font2);
Font font3 = wb.createFont();
font3.setFontHeightInPoints((short)18);
font3.setFontName("Times New Roman");
font3.setColor(IndexedColors.LAVENDER.getIndex());
CellStyle style3 = wb.createCellStyle();
style3.setFont(font3);
Font font4 = wb.createFont();
font4.setFontHeightInPoints((short)18);
font4.setFontName("Wingdings");
font4.setColor(IndexedColors.GOLD.getIndex());
CellStyle style4 = wb.createCellStyle();
style4.setFont(font4);
Font font5 = wb.createFont();
font5.setFontName("Symbol");
CellStyle style5 = wb.createCellStyle();
style5.setFont(font5);
XSSFCell cell0 = sheet.createRow(0).createCell(1);
cell0.setCellValue("Default");
cell0.setCellStyle(style0);
XSSFCell cell1 = sheet.createRow(1).createCell(1);
cell1.setCellValue("Courier");
cell1.setCellStyle(style1);
XSSFCell cell2 = sheet.createRow(2).createCell(1);
cell2.setCellValue("Arial中文内容");
cell2.setCellStyle(style2);
XSSFCell cell3 = sheet.createRow(3).createCell(1);
cell3.setCellValue("Times New Roman");
cell3.setCellStyle(style3);
XSSFCell cell4 = sheet.createRow(4).createCell(1);
cell4.setCellValue("Wingdings");
cell4.setCellStyle(style4);
XSSFCell cell5 = sheet.createRow(5).createCell(1);
cell5.setCellValue("Symbol");
cell5.setCellStyle(style5);
// Write the output to a file
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(path);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
XlsxUtil test = new XlsxUtil();
test.writeExcel2007("C:\Documents and Settings\不了了之\桌面\2007.xlsx");
}
}