POI 操作office2007

初秋的大连下午也困的要命,不知道是不是因为昨天睡觉太晚了。闲来没事逛了一下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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值