POI 读取Excel2003、2007的一个Demo

package poi;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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;

public class ExcelReadDemo {
	 

	public static void main(String[] args) throws IOException {
		
		
		excel2003("C:/Users/lastone/Desktop/test.xls");
		//excelX("C:/Users/lastone/Desktop/test.xlsx");
	}
	/**
	 * 	     支持	Excel2003、2007
	     *@Title: excelX
	     *@Description: 
	     *@添加时间:2014-7-21
	     *@author : fdcxy
	 */
	public static void excelX(String fStr) throws IOException {
		FileInputStream fis	  = new  FileInputStream( fStr );  
		String			ext	  = fStr.substring(fStr.lastIndexOf('.')+1);
		Workbook		wb	  = null;
		Sheet			sheet =	null;
		
		//得到Excel工作簿对象    
		if("xls".equals(ext))
			wb 	  = new  HSSFWorkbook(fis);  
		else if("xlsx".equals(ext))
			wb	  = new  XSSFWorkbook(fis);
		
		
		//得到Excel工作表对象    
		sheet = wb.getSheetAt(0); 
		
		int rowStartNum = (sheet.getFirstRowNum());
		int rowEndNum   = (sheet.getLastRowNum()+1);
		
		for(int i=rowStartNum;i<rowEndNum;i++){
				//得到Excel工作表的行    
				Row row = sheet.getRow(i);  
				System.out.print((i+1)+"\t");//行标

				int	 firstNum =	row.getFirstCellNum();
				int	 endNum	  = row.getLastCellNum();
				for(int j=firstNum;j<endNum;j++){
					//得到Excel工作表指定行的单元格    
					Cell cell = row.getCell(j);  
					//CellStyle cellStyle = cell.getCellStyle();//得到单元格样式 
					if(cell!=null){
						//内容格式
						if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC)
							System.out.print(cell.getNumericCellValue()+"\t");
						else if(cell.getCellType()==Cell.CELL_TYPE_STRING)
							System.out.print(cell.getStringCellValue()+"\t");
					}
				}
				System.out.println();
		}
		System.out.println("rows:"+rowEndNum+"行");
	}
	
	/**
	 * 	     支持	Excel2003
	     *@Title: excel2003
	     *@Description: 
	     *@添加时间:2014-7-21
	     *@author : fdcxy
	 */
	public static void excel2003(String fStr) throws IOException {
		FileInputStream fis  	 = 	new  FileInputStream( fStr );  
		//得到Excel工作簿对象    
		HSSFWorkbook 	wb		 = 	new  HSSFWorkbook(fis);  
		//得到Excel工作表对象    
		HSSFSheet 		sheet 	 = 	wb.getSheetAt(0);   
		
		int rowStartNum = (sheet.getFirstRowNum());
		int rowEndNum   = (sheet.getLastRowNum()+1);
		
		for(int i=rowStartNum;i<rowEndNum;i++){
			//得到Excel工作表的行    
			HSSFRow row = sheet.getRow(i);  
			System.out.print((i+1)+"\t");//行标
			
			int		firstNum =	row.getFirstCellNum();
			int		endNum	  = row.getLastCellNum();
			for(int j=firstNum;j<endNum;j++){
				//得到Excel工作表指定行的单元格    
				HSSFCell cell = row.getCell(j);  
				//CellStyle cellStyle = cell.getCellStyle();//得到单元格样式 
				if(cell!=null){
					if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC)
						System.out.print(cell.getNumericCellValue()+"\t");
					else if(cell.getCellType()==Cell.CELL_TYPE_STRING)
						System.out.print(cell.getStringCellValue()+"\t");
				}
			}
			System.out.println();
		}
		System.out.println("rows:"+rowEndNum+"行");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以帮您提供一个poi合并多个excel文件的demo。具体操作可以参考以下代码: ``` import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; 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; public class MergeExcelDemo { public static void main(String[] args) { try { //获取要合并的所有excel文件 String folderPath = "D:\\excel\\"; File folder = new File(folderPath); File[] files = folder.listFiles(); //创建新的工作薄 Workbook mergedWorkbook = null; boolean isXlsx = true; //是否为.xlsx格式 String mergedFilePath = "D:\\excel\\merged.xlsx"; if (mergedFilePath.endsWith(".xls")) { isXlsx = false; mergedWorkbook = new HSSFWorkbook(); } else if (mergedFilePath.endsWith(".xlsx")) { mergedWorkbook = new XSSFWorkbook(); } else { System.out.println("不支持的文件格式!"); return; } //循环读取并合并所有excel文件 for (File file : files) { FileInputStream inputStream = new FileInputStream(file); Workbook workbook = null; if (isXlsx) { workbook = new XSSFWorkbook(inputStream); } else { workbook = new HSSFWorkbook(inputStream); } //循环所有工作表 for (int i = 0; i < workbook.getNumberOfSheets(); i++) { Sheet sheet = workbook.getSheetAt(i); Sheet mergedSheet = mergedWorkbook.createSheet(sheet.getSheetName()); //循环所有行 int rowIndex = 0; for (Row row : sheet) { Row mergedRow = mergedSheet.createRow(rowIndex); //循环所有单元格 int columnIndex = 0; for (Cell cell : row) { Cell mergedCell = mergedRow.createCell(columnIndex); mergedCell.setCellValue(cell.getStringCellValue()); columnIndex++; } rowIndex++; } } inputStream.close(); } //输出合并后的文件 FileOutputStream outputStream = new FileOutputStream(mergedFilePath); mergedWorkbook.write(outputStream); outputStream.close(); System.out.println("合并成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 使用该demo前,请先引入以下依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值