POI 操作office2007
来源:互联网 发布:手机淘宝海报在线制作 编辑:程序博客网 时间:2024/06/07 13:23
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
- POI 操作office2007
- POI读写Office2007
- poi操作
- POI操作
- POI操作
- ASP.Net在线操作Office2007之解决方案
- ASP.Net在线操作Office2007之解决方案
- 利用Java操作Office2007成为可能
- 安装Office2007主互操作程序集
- Excel文件office2003 and office2007 操作
- POI操作EXCEL
- POI操作EXCEL
- POI操作WORD
- POI操作Excel文档
- 使用POI操作Excel
- poi操作excel文件
- poi操作excel文件
- poi 操作Excel
- Linux下TFTP服务的安装、配置和操作
- Java中重载与重写的区别
- js 回调函数
- android 电容屏(一):电容屏基本原理篇
- PackageManager使用
- POI 操作office2007
- POJ 2387 Til the Cows Come Home(简单的Dijkstra算法)
- cycript脚本例子
- mysql 基本命令
- 3295: [Cqoi2011]动态逆序对 CDQ分治
- Spring的任务调度
- php数组的数据结构
- XMPP之好友获取
- MD5加密