POI 操作office2007
2016-03-01 11:20
417 查看
XSSFWorkbook与HSSFWorkbook的差别
api基本一致 操作office2007部分代码如下:
Java代码
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代码
Java代码
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();
}
api基本一致 操作office2007部分代码如下:
Java代码
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代码
Java代码
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();
}
相关文章推荐
- hadoop伪分布式ha框架搭建
- android 电容屏(一):电容屏基本原理篇
- 我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
- Linux下TFTP服务的安装、配置和操作
- 2016年Android市场的8个大胆预测
- Java中重载与重写的区别
- 31.绿豆蛙的归宿(拓扑排序)
- 31.绿豆蛙的归宿(拓扑排序)
- 31.绿豆蛙的归宿(拓扑排序)
- 31.绿豆蛙的归宿(拓扑排序)
- 31.绿豆蛙的归宿(拓扑排序)
- 31.绿豆蛙的归宿(拓扑排序)
- iOS presentViewController方式跳转延迟
- iOS多线程
- 向设计小组成员传达开发背景信息, 从中我能收获些什么?
- 分页查询
- Android WebView 踩过的坑
- 【项目管理和构建】——Maven下载、安装和配置(二)
- .net如何处理高并发socket,建立高性能健壮的socket服务
- html + js 右 点击 弹出 菜单