初秋的大连下午也困的要命,不知道是不是因为昨天睡觉太晚了。闲来没事逛了一下apache,看到POI就拿来小试一下。开始没搞明白offiice2003和2007这么大差别,网上也有人乱七八糟说一通,小弟再用office2007就拿他开刀,我看官网demo是2003。比如excel吧是XSSFWorkbook是HSSFWorkbook的差别,api基本一致。
操作office2007部分代码如下:
public class Excel
{
//读excel文档
public void readExcel(String sheetName)
{
XSSFSheet sheet = getSheet(sheetName);
if(sheet != null)
{
for(Iterator<Row> i = sheet.rowIterator(); i.hasNext();)
{
//Row row = i.next();
XSSFRow row = (XSSFRow) i.next();
if(row == null)
continue;
//System.out.println(row.getRowNum() + "="+row.getCell(0));
for(Iterator<Cell> j = row.cellIterator(); j.hasNext();)
{
//Cell cell = j.next();
XSSFCell cell = (XSSFCell) j.next();
if(cell == null)
continue;
System.out.print(getCellValue(cell) + " ");
}
System.out.println();
}
}
else
{
System.out.println("没有找到工作表");
}
}
//加载工作薄
private XSSFWorkbook getXSSFWorkBook()
{
XSSFWorkbook workbook = null;
try
{
String path = System.getProperty("user.dir") + System.getProperty("file.separator")+"doc"+ System.getProperty("file.separator")+"ExcelDoc.xlsx";
workbook = new XSSFWorkbook(new FileInputStream(path));
}
catch (Exception e)
{
e.printStackTrace();
}
return workbook;
}
//根据名字取工作表Sheet
private XSSFSheet getSheet(String sheetName)
{
return this.getXSSFWorkBook().getSheet(sheetName);
}
//判断Cell单元格的类型。
private String getCellValue(Cell cell)
{
Object result = null;
switch(cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellInternalDateFormatted(cell))
result = cell.getDateCellValue();
else
result = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
result = cell.getCellFormula();
break;
case Cell.CELL_TYPE_ERROR:
result = cell.getErrorCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
default:
result = "NULL";
break;
}
return result.toString();
}
//生成excel文件
public void createExcelDoc()
{
String path = System.getProperty("user.dir") + System.getProperty("file.separator")+"doc"+ System.getProperty("file.separator")+"creatExcelDoc.xlsx";
try
{
FileOutputStream outStream = new FileOutputStream(path);
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("first");
sheet.autoSizeColumn(0);
XSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("Name");
row.createCell(1).setCellValue(2.2);
//helper.createDataFormat() 得到一个DataFormat实例
/*CreationHelper helper = workbook.getCreationHelper();
CellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss"));
Cell c = row.createCell(2);
c.setCellValue(new Date());
c.setCellStyle(dateStyle);*/
//XSSF....方法
XSSFCell cell = row.createCell(2);
XSSFCellStyle style = workbook.createCellStyle();
XSSFCreationHelper h = workbook.getCreationHelper();
style.setDataFormat(h.createDataFormat().getFormat("yyyy-MM-dd"));
style.setAlignment(XSSFCellStyle.VERTICAL_CENTER);
cell.setCellStyle(style);
cell.setCellValue(new Date());
//设置第n单元格的宽度,自动
sheet.autoSizeColumn(2);
workbook.write(outStream);
System.out.println("create finished!!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
word2007代码
public void readWord()
{
try
{
String path = System.getProperty("user.dir") + System.getProperty("file.separator")+"doc"+ System.getProperty("file.separator")+"WordDoc.docx";
XWPFDocument document = new XWPFDocument(new FileInputStream(path));
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
System.out.println(document.getFootnotes().size());
System.out.println(document.getDocument().getBody());
System.out.println(extractor.getText());
System.out.println(extractor.getMetadataTextExtractor().getText().toUpperCase());
}
catch (Exception e)
{
e.printStackTrace();
}
分享到:
相关推荐
今天用POI解析了excel,现在整理了一下,附件中包含POI操作2007office需要用到的所有jar包 dom4j-1.6.1.jar geronimo-stax-api_1.0_spec-1.0.jar ooxml-schemas-1.0.jar openxml4j-bin-beta.jar poi-3.0-FINAL.jar ...
poi 最新版本 支持office2007的操作 XSSF类支持Excel 2007 操作
下载后,运行 可以 Test1中的main方法,注意里面的word模版地址要修改下。...test1.java主要是针对word的书签操作 MSWordTool.java主要是针对word的列表书签的操作,相对来说比较复杂,具体看个人需求。
poi操作office
POI操作EXCEL,支持office2003,2007,2010,2013的.xls和.xlsx文件(详)
java生成excel 操作word jacob java操作office poi jacob
apache poi 操作office文档的,linux和windows双版本。仅仅是记录一下自己使用过的资源。
POI是Apache的一个子项目,主要是利用java的API操作excel,word等MS Office Software,资源内部包涵excel的03版本和07版本了
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。3.11 支持office 2007等。
JAVA操作MS office 工具 POI操作WORD 官方测试案例 DEMO(含jar包,IDEA) 可直接运行测试。 ------------------------------------------------------ 1 什么是Apache POI 全称Apache POI,使用Java编写的免费...
poi 操作 word 报表,兼容office 和 wps
Apache poi 是Apache在java与Office相关文档进行交互操作的一个开源包 可以轻松的能过java与word或excel进行操作
《apache poi4.0》【最新版poi,snapshot版本,新增多种操作office文件的功能】.docx
poi用于操作常用的office软件,如excel,word,txt等,poi支持office2007及以上版本,而jxl只能操作office2003及以下版本
附件是java poi 3.17的lib库及其依赖库,全部下载引用后,可以进行excel的读写操作,无需引用其他lib库。poi-3.17适用于jdk1.7。 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft ...
主要用于java操作office api 文档 POI API 3.12.chm
POI 3.8(包含源码,操作OFFICE所需的所有JAR包和相关API)
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 结构: HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft Excel ...
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
java操作poi jar包下载,java操作office的插件