java-poi3.17读取excel文本和图片
2018-03-17 23:26
471 查看
package per.qy.dexter.fileoperate; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.UUID; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.PictureData; 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.junit.Test; public class ExcelTest { @Test public void testExcel() { String path = "D:\\temp\\temp\\test.xls"; // String path = "D:\\temp\\temp\\test.xlsx"; File file = new File(path); InputStream is = null; Workbook workbook = null; try { is = new FileInputStream(file); if (path.endsWith(".xls")) { workbook = new HSSFWorkbook(is); } else if (path.endsWith(".xlsx")) { workbook = new XSSFWorkbook(is); } if (workbook != null) { int sheetCount = workbook.getNumberOfSheets(); if (sheetCount > 0) { // 文本内容 StringBuilder content = new StringBuilder(); for (int i = 0; i < sheetCount; i++) { Sheet sheet = workbook.getSheetAt(i); content.append(sheet.getSheetName()); for (int rownum = sheet.getFirstRowNum(); rownum <= sheet.getLastRowNum(); rownum++) { Row row = sheet.getRow(rownum); if (row == null || row.getFirstCellNum() < 0) { break; } for (int columnnum = row.getFirstCellNum(); columnnum <= row .getLastCellNum(); columnnum++) { String cellValue = getCellValue(row.getCell(columnnum)); content.append(cellValue); if (content.length() > 500) {// 没500字输出一次 System.out.println(content.toString()); content.delete(0, content.length()); } } } } if (content.length() > 0) { System.out.println(content.toString()); } // 图片内容 List<?> pictures = workbook.getAllPictures(); if (pictures != null && !pictures.isEmpty()) { for (int i = 0; i < 94cc ; pictures.size(); i++) { PictureData picture = (PictureData) pictures.get(i); byte[] data = picture.getData(); FileOutputStream out = new FileOutputStream( "D:\\temp\\temp\\" + UUID.randomUUID() + ".jpg"); out.write(data); out.close(); } } } } } catch (FileNotFoundException e) { } catch (IOException e) { } finally { try { if (workbook != null) { workbook.close(); } if (is != null) { is.close(); } } catch (IOException e) { } } } private String getCellValue(Cell cell) { if (cell == null) { return ""; } // 都按文本格式读取 cell.setCellType(CellType.STRING); return cell.getStringCellValue(); } }
相关文章推荐
- java-poi3.17读取ppt文本和图片
- java-poi3.17读取word文本及图片
- JAVA 通过poi 读取 本地excel(.xls,.xlsx)文件,文件中 包含图片
- java/poi读取word,并替换word中的文本内容,向word中插入图片的操作
- java/poi读取word,并替换word中的文本内容,向word中插入图片的操作
- Java 操作 Excel (读取Excel2007,Poi实现)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java 操作 Excel (读取Excel2003 2007,Poi实现)
- JAVA POI自动导出数据和图片到excel
- Java的poi技术读取和导入Excel
- java poi读取excel操作示例(2个代码)
- java的poi技术读取和写入excel
- Java POI 读取Office excel (2003,2007)及相关jar包
- java poi读取excel日期格式数据
- java POI实现向Excel中插入图片
- Java的poi技术一-------------读取excel到MySql
- Java利用POI 读取Excel行列数,getLastRowNum()和getLastCellNum()的区别
- java POI导出excel(带有图片的数据)以及ftp上传图片到另一台服务器的方法参考代码
- java的poi技术读取Excel[xls,xlsx]
- poi,java向 excel文件写数据(缺点,HSSFWorkbook不支持图片) 例子