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();
		}

你可能感兴趣的:(C++,c,Excel,C#,J#)