excel合并含合并同类项的导出
来源:互联网 发布:淘宝怎么搜二手 编辑:程序博客网 时间:2024/04/30 09:50
package com.eastcom_sw.inas.common.utils.excel;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;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.poifs.filesystem.POIFSFileSystem;import org.springframework.web.multipart.commons.CommonsMultipartFile;/** * 用于读取excel */public class ExcelReader1 { private HSSFWorkbook wb = null;//Excel文档对象 private HSSFSheet sheet = null;//Excel表单对象 private HSSFRow row = null;//Excel行 private int sheetNum = 0;//第1个工作表 private int rowNum = 0; private InputStream fis = null; private CommonsMultipartFile file = null; public ExcelReader1() { } public ExcelReader1(CommonsMultipartFile file) { this.file = file; } public void setRowNum(int rowNum) { this.rowNum = rowNum; } public void setSheetNum(int sheetNum) { this.sheetNum = sheetNum; } public void setFile(CommonsMultipartFile file) { this.file = file; } /** * 读取excel文件获得HSSFWorkbook对象 */ public void open() throws IOException {// fis = new FileInputStream(file); fis = file.getInputStream(); wb = new HSSFWorkbook(new POIFSFileSystem(fis)); fis.close(); } /** * 返回sheet表数目 * * @return int */ public int getSheetCount() { int sheetCount = -1; sheetCount = wb.getNumberOfSheets(); return sheetCount; } /** * sheetNum下的记录行数 * * @return int */ public int getRowCount() { if (wb == null) System.out.println("=============>WorkBook为空"); HSSFSheet sheet = wb.getSheetAt(this.sheetNum); int rowCount = -1; rowCount = sheet.getLastRowNum(); return rowCount; } /** * 读取指定sheetNum的rowCount * * @param sheetNum * @return int */ public int getRowCount(int sheetNum) { HSSFSheet sheet = wb.getSheetAt(sheetNum); int rowCount = -1; rowCount = sheet.getLastRowNum(); return rowCount; } /** * 得到指定行的内容 * * @param lineNum * @return String[] */ public String[] readExcelLine(int lineNum) { return readExcelLine(this.sheetNum, lineNum); } /** * 指定工作表和行数的内容 * * @param sheetNum * @param lineNum * @return String[] */ public String[] readExcelLine(int sheetNum, int lineNum) { if (sheetNum < 0 || lineNum < 0) return null; String[] strExcelLine = null; try { sheet = wb.getSheetAt(sheetNum); row = sheet.getRow(lineNum); int cellCount=0; if(!isBlankRow(row)){ cellCount = row.getLastCellNum();//获得最后一个单元格的索引 } strExcelLine = new String[cellCount + 1]; for (int i = 0; i < cellCount; i++) { strExcelLine[i] = readStringExcelCell(lineNum, i); } } catch (Exception e) { e.printStackTrace(); } return strExcelLine; } /** * 读取指定列的内容 * * @param cellNum * @return String */ public String readStringExcelCell(int cellNum) { return readStringExcelCell(this.rowNum, cellNum); } /** * 指定行和列编号的内容 * * @param rowNum * @param cellNum * @return String */ public String readStringExcelCell(int rowNum, int cellNum) { return readStringExcelCell(this.sheetNum, rowNum, cellNum); } /** * 指定工作表、行、列下的内容 * * @param sheetNum * @param rowNum * @param cellNum * @return String */ public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) { if (sheetNum < 0 || rowNum < 0) return ""; String strExcelCell = ""; try { sheet = wb.getSheetAt(sheetNum); row = sheet.getRow(rowNum); if(row.getCell((short)cellNum) != null){ switch (row.getCell((short) cellNum).getCellType()) { case HSSFCell.CELL_TYPE_FORMULA://如果是公式型 strExcelCell = "FORMULA "; break; case HSSFCell.CELL_TYPE_NUMERIC: //如果是数字型 strExcelCell = String.valueOf(row.getCell((short) cellNum).getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING://如果是字符串型 strExcelCell = row.getCell((short) cellNum).getStringCellValue(); break; case HSSFCell.CELL_TYPE_BOOLEAN://如果是布尔型 strExcelCell=String.valueOf(row.getCell((short) cellNum).getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_BLANK://如果是空串 strExcelCell = ""; break; case HSSFCell.CELL_TYPE_ERROR://如果是错误型 strExcelCell = String.valueOf(row.getCell((short) cellNum).getErrorCellValue()); break; default: strExcelCell = ""; break; } } else{ strExcelCell = ""; } } catch (Exception e) { e.printStackTrace(); } return strExcelCell; } //判断指定行单元格是否为空 public static boolean isBlankRow(HSSFRow row){ if(row == null) return true; boolean result = true; for(int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++){ HSSFCell cell = row.getCell(i, HSSFRow.RETURN_BLANK_AS_NULL); String value = ""; if(cell != null){ switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING://如果是字符串型 value = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC://如果是数字型 value = String.valueOf((int) cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN://如果是布尔型 value = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA://如果是公式型 value = String.valueOf(cell.getCellFormula()); break; case HSSFCell.CELL_TYPE_BLANK://如果是空串 value=""; break; case HSSFCell.CELL_TYPE_ERROR://如果是错误型 value = String.valueOf(cell.getErrorCellValue()); default://其他类型 value=""; break; } } if(!value.trim().equals("")){ result = false; break; } } return result; } // 主函数用于测试 public static void main(String args[]) {// File file = new File("E:\\text.xls");// CommonsMultipartFile file1 = new CommonsMultipartFile("E:\\text.xls");// ExcelReader1 readExcel = new ExcelReader1(file1);// try {// readExcel.open();// } catch (IOException e) {// e.printStackTrace();// }// readExcel.setSheetNum(0); //设置读取索引为0的工作表// //总行数// int count = readExcel.getRowCount();// for (int i = 0; i <= count; i++) {// String[] rows = readExcel.readExcelLine(i);// if(rows.length>0){// for (int j = 0; j < rows.length; j++) {// if(!(rows[j]==null)){// System.out.print(rows[j] + " ");// }// }// }// System.out.print("\n");// }}}
0 0
- excel合并含合并同类项的导出
- 合并数据库同类项
- 二元多项式同类项合并问题
- 苏州OJ c001: 合并同类项
- 字符的去重操作~合并同类项操作
- poi导出excel,支持合并的单元格
- poi导出excel合并单元格(包括列合并、行合并)
- jxl合并单元格导出excel
- POI导出Excel--合并单元格
- Java导出Excel合并单元格
- POI导出Excel 合并单元格
- POI导出excel合并单元格
- Java导出Excel合并单元格
- java导出Excel合并单元格
- POI导出Excel--合并单元格
- js实现HTML Table 导出为Excel格式(含合并列格式)
- C#合并excel单元格,操作合并后的单元格 (winform导出excel)
- 导出的excel同一列相同内容的合并
- 笔记 <Core Java2 卷1>第四章 对象和类
- 挑战年薪50万,你可以的
- JS之三种对话框
- Hadoop: the definitive guide 第三版 拾遗 第四章 之SequenceFile操作
- 有关protobuf
- excel合并含合并同类项的导出
- 一个App调用另一个App的Activity的方法
- Hadoop: the definitive guide 第三版 拾遗 第四章 之MapFile
- 安装cdh4.5工具cloudera manager
- vs2013设置winpcap开发环境
- 图的搜索算法
- matlab 同个坐标下的两个函数图像的比较
- HDU2084
- android 手机不能发短信