`
happyqing
  • 浏览: 3149718 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java用poi操作excel .xls和.xlsx格式

    博客分类:
  • poi
阅读更多

 

jxl主页

http://www.andykhan.com/jexcelapi/index.html

jxl似乎不支持2007,2010

jxl读取excel2010报错
jxl.read.biff.BiffException: Unable to recognize OLE stream
 at jxl.read.biff.CompoundFile.<init>(CompoundFile.java:116)
 at jxl.read.biff.File.<init>(File.java:127)
 at jxl.Workbook.getWorkbook(Workbook.java:221)
 at jxl.Workbook.getWorkbook(Workbook.java:198)
 at ReadExcel.readExcel(ReadExcel.java:22)
 at ReadExcel.main(ReadExcel.java:64)

 

poi主页

http://poi.apache.org/download.html

建议使用poi,支持2003,2007,2010等,有更新

 

poi读取excel2010报错
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
 at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:131)
 at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
 at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
 at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:322)
 at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:303)
 at ExcelTo.main(ExcelTo.java:12)

读取2007,2010格式的应该用XSSF

 

poi读取的例子

package com.urt.module.excel;

import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.DateUtil;
public class ExcelPoi {
	public static void main(String[] args) {
		String fileToBeRead = "D:\\test.xlsx";
		Workbook workbook;
		try {
			if(fileToBeRead.indexOf(".xlsx")>-1){
				workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead));
			} else {
				workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
			}
			//HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); //2003 创建对Excel工作簿文件的引用
			//XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead)); //2007,2010 创建对Excel工作簿文件的引用
			Sheet sheet = workbook.getSheet("Sheet1"); // 创建对工作表的引用
			int rows = sheet.getPhysicalNumberOfRows();// 获取表格的
			int columns = 0;
			for (int r = 0; r < rows; r++) { // 循环遍历表格的行
				if(r==0){
     
					//在第一行标题行计算出列宽度,因为数据行中可能会有空值
     
					columns = sheet.getRow(r).getLastCellNum();
    
					continue;
    
				}
				String value = "";
				Row row = sheet.getRow(r); // 获取单元格中指定的行对象
				if (row != null) {
					//int cells = row.getPhysicalNumberOfCells();// 获取一行中的单元格数
     
					//int cells = row.getLastCellNum();// 获取一行中最后单元格的编号(从1开始)					
					for (short c = 0; c < columns; c++) { // 循环遍历行中的单元格
						Cell cell = row.getCell((short) c); 
						if (cell != null) {
							if (cell.getCellType() == Cell.CELL_TYPE_STRING) { // 判断单元格的值是否为字符串类型
								value += cell.getStringCellValue() + ",";
							} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { // 判断单元格的值是否为数字类型
								//if(DateUtil.isCellDateFormatted(cell)){
         
								//	cell.getDateCellValue();
       //日期型 
								//}								
								value += cell.getNumericCellValue() + ",";
							} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { // 判断单元格的值是否为布尔类型
								value += cell.getStringCellValue() + ",";
							}
						}
					}
				}
				String[] str = value.split(",");
				System.out.println(value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

得到单元格的字符串内容,注意:有的excel里有隐藏列

//得到单元格的字符串内容
	public static String getCellValue(Cell cell) {
		DecimalFormat df = new DecimalFormat("#");
		if (cell == null)
			return "";
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_NUMERIC:
			if(DateUtil.isCellDateFormatted(cell)){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				return sdf.format(cell.getDateCellValue()).toString();
				//return sdf.format(DateUtil.getJavaDate(cell.getNumericCellValue())).toString();
			}
			return df.format(cell.getNumericCellValue());
		case Cell.CELL_TYPE_STRING:
			//System.out.println(cell.getStringCellValue());
			return cell.getStringCellValue();
		case Cell.CELL_TYPE_FORMULA:
			return cell.getCellFormula();
		case Cell.CELL_TYPE_BLANK:
			return "";
		case Cell.CELL_TYPE_BOOLEAN:
			return cell.getBooleanCellValue() + "";
		case Cell.CELL_TYPE_ERROR:
			return cell.getErrorCellValue() + "";
		}
		return "";
	}

 

poi导出excel,不使用模板的

http://happyqing.iteye.com/blog/2075985

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics