java处理Excel
来源:互联网 发布:小岛秀夫离职原因知乎 编辑:程序博客网 时间:2024/06/05 10:54
1,得到上传的文件
FileItem upfile = parser.getParameters().getFileItem("upfile");
等类似方法
2,转成输入流
InputStream is = upfile.getInputStream();
3,通过输入流构建workbook
Workbook wb = WorkbookFactory.create(is);
maven依赖:
<!-- 微软office处理 api --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.14</version> <exclusions> <exclusion> <groupId>stax</groupId> <artifactId>stax-api</artifactId> </exclusion> </exclusions> </dependency> <!-- 微软office处理 api end -->
4,常用方法:
Sheet sheet = wb.getSheetAt(0) : 取第一个sheet页
int lastRowIndex = sheet.getLastRowNum(); 得到行数
workbook = new XSSFWorkbook(); 生成xlsx文件
workbook = new HSSFWorkbook();生成xls文件
sheet = workbook.createSheet(sheetName); 新建一页
/** * 初始化样式 */ private void initStyle() { // 设置表头样式 headerStyle = workbook.createCellStyle(); headerStyle.setWrapText(true); headerStyle.setBorderTop(CellStyle.BORDER_THIN); headerStyle.setBorderBottom(CellStyle.BORDER_THIN); headerStyle.setBorderLeft(CellStyle.BORDER_THIN); headerStyle.setBorderRight(CellStyle.BORDER_THIN); headerStyle.setAlignment(CellStyle.ALIGN_CENTER); headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); headerStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); Font font = workbook.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 12); headerStyle.setFont(font); // 设置单元格样式 cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(true); cellStyle.setBorderTop(CellStyle.BORDER_THIN); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); }
/** * 根据inputStream创建Excel文件(用于读取Excel数据) * * @param inputStream * @throws IOException */ public NewExcelHelper(InputStream inputStream, boolean isNew) throws IOException { if (isNew) { workbook = new XSSFWorkbook(inputStream); } else { workbook = new HSSFWorkbook(inputStream); } sheet = workbook.getSheetAt(0); }
/** * 获取单元格 * * @param rownum * @param column * @return */ public Cell getCell(int rownum, short column) { row = sheet.getRow(rownum); if (row == null) { return null; } return row.getCell(column); }
0 0
- java处理Microsoft Excel
- java处理Microsoft Excel
- Java 处理Excel
- java 处理excel
- Java处理EXCEL
- java处理excel
- java中处理Excel
- java处理Excel
- java jxl处理excel
- java处理excel文件
- java处理Excel
- Java处理Excel文件
- java处理excel(java使用Apache POI处理Excel)
- java处理Excel(POI包)
- java处理 Excel(POI包)
- java处理Excel(POI包)
- Java处理Excel,csv文件
- java处理Excel相关笔记
- iOS自己总结的超级详细分解富文本大全(AttributedString),图文混排很轻松
- python中的断言assert
- Scanner获取键盘输入
- 无
- 我的第一个FPGA小程序---测占空比
- java处理Excel
- CentOS安装后ifconfig 无法显示网卡
- android中String与InputStream之间的相互转换方式
- 二叉树中的DFS和BFS
- JS Location & History & Screen
- 【bzoj1005】 HNOI2008—明明的烦恼
- 和为S的连续正数序列
- 以声明方式在一个标题窗格内添加调色板的代码片段
- CAN总线