JAVA 通过poi 读取 本地excel(.xls,.xlsx)文件,文件中 包含图片
来源:互联网 发布:动漫设计软件 编辑:程序博客网 时间:2024/05/16 03:31
以下代码亲测可以使用。以下代码可以直接运行查看效果。
jar 下载地址:http://download.csdn.net/detail/qw0907/9741548
用的jar如下:
poi-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
poi-scratchpad-3.8-20120326.jar
读取.xls
import java.io.File;import java.io.FileInputStream;import java.util.List; import org.apache.poi.hssf.usermodel.HSSFPicture;import org.apache.poi.hssf.usermodel.HSSFPictureData;import org.apache.poi.hssf.usermodel.HSSFShape;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.DateUtil;import org.apache.poi.ss.usermodel.Row; public final class TestImportExcel { public static void main(String[] args) throws Exception { File excelFile = new File("F:\\B5204645.xls"); HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile)); HSSFSheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.print(cell.getRichStringCellValue().getString()); System.out.print("|"); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.print(String.valueOf(cell.getDateCellValue())); } else { System.out.print(cell.getNumericCellValue()); } System.out.print("|"); break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue()); System.out.print("|"); break; default: } } System.out.println(); } //读取图片 List<HSSFPictureData> pictures = wb.getAllPictures(); for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { if (shape instanceof HSSFPicture) { HSSFPicture pic = (HSSFPicture) shape; int pictureIndex = pic.getPictureIndex()-1; HSSFPictureData picData = pictures.get(pictureIndex); System.out.println("image-size:" + picData.getData().length); } } System.out.println(wb.getSheetName(0)); }}
读取.xlsx
package com.sae.ecds.ct.excel;import java.io.File;import java.io.FileInputStream;import java.util.List; import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.DateUtil;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFPictureData;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook; public final class TestImportXlsx { public static void main(String[] args) throws Exception { File excelFile = new File("F:\\B52046450056.xlsx"); XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(excelFile)); XSSFSheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.print(cell.getRichStringCellValue().getString()); System.out.print("|"); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.print(String.valueOf(cell.getDateCellValue())); } else { System.out.print(cell.getNumericCellValue()); } System.out.print("|"); break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue()); System.out.print("|"); break; default: } } System.out.println(); } //读取图片 List<XSSFPictureData> pictures = wb.getAllPictures(); for (int i = 0; i < pictures.size(); i++) { XSSFPictureData pictureData = pictures.get(i); byte[] picData = pictureData.getData(); System.out.println("image-size:" + picData.length); } System.out.println(wb.getSheetName(0)); }}
1 0
- JAVA 通过poi 读取 本地excel(.xls,.xlsx)文件,文件中 包含图片
- 通过poi实现解析并读取excel文件(包含xls、xlsx后缀)
- 通过poi实现解析并读取excel文件(包含xls、xlsx后缀)
- POI读取EXCEL下xlsx,xls文件
- java:poi读取excel文件,分xls和xlsx
- java使用POI读取excel文件,兼容xls和xlsx
- Java 读取excel xls以及xlsx文件
- Java通过apache poi 读取excel(.xlsx)文件,并通过MyBbtis插入数据库中
- Java中通过POI读取Excel 2003 - 2007的xls,xlsx格式
- java使用POI实现excel文件的读取,兼容后缀名xls和xlsx
- java使用poi读取xls格式和xlsx格式的excel文件
- Java 实现导出excel表 POI/Java实现不同excel格式(*.xls、*.xlsx)文件的读取
- java 读取excel文件包括( XX.xls、XX.xlsx)
- JAVA读取xls文件和xlsx文件
- JAVA读取xls文件和xlsx文件
- org.apache.poi读取后缀为.xls和.xlsx的excel文件
- java的poi技术读取Excel[xls,xlsx]
- pio java读取xls和xlsx文件
- linux文件压缩及解压
- qt opengl lesson1 建立一个opengl窗口
- MIPI接口
- makefile和bash结合编译-执行(入门)
- 相对定位和绝对定位的区别
- JAVA 通过poi 读取 本地excel(.xls,.xlsx)文件,文件中 包含图片
- Oracle卸载
- Android之旅第一站——第一行代码…
- Java微信公众平台开发之OAuth2.0网页授权
- ZCMU—1798
- 布局
- 把第一次写的题解转到这里来了
- unity3d开发 打飞机小游戏(三)(飞机子弹设置)
- linux中网络配置与修改