import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Font; 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.ss.usermodel.WorkbookFactory; /** * 操作Excel表格的功能类 */ public class ExcelReader { private Workbook wb; private Sheet sheet; private Row row; /** * 读取Excel表格表头的内容 * @param InputStream * @return String 表头内容的数组 * @throws InvalidFormatException */ public String[] readExcelTitle(InputStream is) throws InvalidFormatException { try { wb = WorkbookFactory.create(is); } catch (IOException e) { e.printStackTrace(); } sheet = wb.getSheetAt(0); row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); System.out.println("colNum:" + colNum); String[] title = new String[colNum]; for (int i = 0; i < colNum; i++) { title[i] = getStringCellValue(row.getCell(i)); } return title; } /** * 读取Excel数据内容 * @param InputStream * @return Map 包含单元格数据内容的Map对象 * @throws InvalidFormatException */ public List<String[]> readExcelContent(InputStream is) throws InvalidFormatException { try { wb = WorkbookFactory.create(is); } catch (IOException e) { e.printStackTrace(); } sheet = wb.getSheetAt(0); int rowNum = sheet.getLastRowNum(); row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); List<String[]> list = new ArrayList<String[]>(); for (int i = 1; i <= rowNum; i++) { row = sheet.getRow(i); String content[] = new String[colNum]; int j = 0; while (j < colNum) { content[j] = this.getStringCellValue(row.getCell(j)); j++; } list.add(content); } return list; } /** * 获取单元格数据内容为字符串类型的数据 * * @param cell Excel单元格 * @return String 单元格数据内容 */ private String getStringCellValue(Cell cell) { String strCell = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: strCell = cell.getStringCellValue().trim(); break; case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)){ strCell = sdf.format(cell.getDateCellValue()); }else{ strCell = String.valueOf(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } return strCell; } /** * 写入内容 * @param is * @param sheetName * @param list * @throws IOException * @throws InvalidFormatException */ public void writeExcelContent(InputStream is,String sheetName,List<String[]> list,OutputStream os) throws Exception{ wb = WorkbookFactory.create(is); sheet= wb.getSheetAt(0); CellStyle style = wb.createCellStyle(); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); style.setAlignment(CellStyle.ALIGN_CENTER); Font font = wb.createFont(); font.setFontHeightInPoints((short) 9); style.setFont(font); for(int i=0;i<list.size();i++){ String[] obj = list.get(i); row = sheet.createRow(i+1); for(int j=0;j<obj.length;j++){ Cell cell = row.createCell(j); cell.setCellStyle(style); cell.setCellValue(obj[j]); } } wb.write(os); os.close(); } public static void main(String[] args) throws Exception{ try { // 对读取Excel表格标题测试 InputStream is = new FileInputStream("d:\\test.xlsx"); ExcelReader excelReader = new ExcelReader(); String[] title = excelReader.readExcelTitle(is); System.out.println("获得Excel表格的标题:"); for (String s : title) { System.out.print(s + " "); } // 对读取Excel表格内容测试 InputStream is2 = new FileInputStream("d:\\test.xlsx"); List<String[]> list = excelReader.readExcelContent(is2); System.out.println("获得Excel表格的内容:"); for (int i = 0; i < list.size(); i++) { String[] s = list.get(i); for(int j=0;j<s.length;j++){ System.out.print(s[j] + " "); } System.out.println("\n"); } } catch (FileNotFoundException e) { System.out.println("未找到指定路径的文件!"); e.printStackTrace(); } } }
/** * * @Title: export * @Description: 导出Excel */ public void export(Context context){ HttpServletRequest request = context.request; HttpServletResponse response = context.response; String filePath = request.getSession().getServletContext().getRealPath("/template/detail.xlsx"); OutputStream os = null; FileInputStream fis = null; try { response.reset(); response.setCharacterEncoding("gb2312"); String fileName = "转账明细.xlsx"; String name = new String(fileName.getBytes("gb2312"), "ISO8859-1"); response.setHeader( "Content-Disposition","attachment; filename=" + name); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); fis = new FileInputStream(filePath); os = response.getOutputStream(); List<String[]> list = service.findAll(); ExcelReader er = new ExcelReader(); er.writeExcelContent(fis, "转账明细", list, os); os.flush(); response.flushBuffer(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { try { if (fis != null) fis.close(); if (os != null) os.close(); } catch (Exception e2) { logger.error(e2.getMessage(), e2); } } }
相关推荐
poi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excel
在本文中,我们将深入探讨如何使用POI进行Excel的读写操作,并进行总结。 1. POI基本概念 Apache POI 提供了HSSF(Horrible Spreadsheet Format)用于读写旧版的.xls格式的Excel文件,而XSSF则用于处理.xlsx格式的...
在这个“POI读写excel文件+poi简单文档”中,我们将深入探讨如何利用Apache POI进行Excel文件的读写操作。 首先,我们需要了解POI的主要组件:HSSF(Horrible Spreadsheet Format)用于处理旧版的BIFF格式(.xls)...
本文将详细介绍如何使用POI库在Java中进行Excel的读写操作,以及一个在Eclipse环境中运行的实例。 首先,我们需要了解Apache POI的基本概念。POI是Apache软件基金会的一个开源项目,它提供了Java API来处理...
遗憾的是,提供的文件列表中只有`jeebbs-db-3.sql`,这似乎是一个SQL数据库文件,与Java POI读写Excel的主题并不直接相关。通常,SQL文件用于导入或导出数据库的数据,如果博客作者有提到结合Java POI与SQL操作Excel...
以下是对"java中poi读写excel封装工具类"这一主题的详细解释。 1. **Apache POI介绍** Apache POI是一个开源项目,允许Java开发者创建、修改和显示Microsoft Office文件,包括Excel、Word和PowerPoint。它的核心...
在这个"POI读写excel(.xls/.xlsx)的Demo"中,我们将深入探讨如何使用Apache POI库在Java中读取和写入Excel文件。 1. **Apache POI库介绍** Apache POI 提供了 HSSF(Horrible Spreadsheet Format)和 XSSF(XML ...
### POI 读写Excel详解 #### 一、概述 Apache POI 是一款非常流行的 Java 库,用于处理 Microsoft Office 格式的文件,包括 Excel 和 Word。本篇内容重点介绍了如何利用 POI 进行 Excel 文件的读写操作,特别适用...
这篇博客“使用POI读写Excel文件(兼容xls与xlsx版本)”深入探讨了如何利用Apache POI库在Java环境中处理Excel文档。 首先,让我们了解一下Apache POI的基本概念。Apache POI提供了一组API,允许开发者创建、修改...
poi读写excel文件,解析xls文件中的内容:得到行数据,返回行数据数组
在标题"java poi 读写Excel jar包"中,我们关注的核心知识点是Java POI库的使用,以及如何在Java程序中处理Excel文件的读写操作。要实现这些功能,你需要下载相应的jar包。描述中提到的链接指向了一个CSDN博客文章,...
在这个“java的poi读写excel项目实例”中,我们有两个主要的Java类:`read.java`和`write.java`,分别用于处理Excel文件的读取和写入操作。 首先,让我们深入了解一下`read.java`。这个类通常会包含一个方法,比如`...
Java使用Apache POI读写Excel是常见的数据处理任务,尤其在数据分析、报表生成等领域中广泛应用。Apache POI是一个开源库,允许程序员创建、修改和显示Microsoft Office格式的文件,其中包括Excel(XLS和XLSX)文件...
1.poi读写excel,需事先引入maven依赖实现方式:* 这个类是用来读写excel文件的,采用的是poi这个jar包实现,当前类只支持对excel200
标题中的“poi 读写excel”指的是Apache POI项目,这是一个流行的开源Java库,用于处理Microsoft Office格式的文件,特别是Excel工作簿。Apache POI提供了一组API,使得开发人员可以方便地在Java应用程序中创建、...
本篇文章将详细介绍如何利用Apache POI与Struts2来实现Excel的读写以及合并多个Excel工作表。 首先,Apache POI提供了HSSF和XSSF两个API,分别用于处理老版本的BIFF格式(.xls)和新版本的OOXML格式(.xlsx)。要...
使用poi读取写入复杂excel内容包括样式,工具类