/** * 将Excel表格转换成TXT文档 * @author wb-quesj * @version 1.0 */ public class ExcelReader { // 创建文件输入流 private BufferedReader reader = null; // 文件类型 private String filetype; // 文件二进制输入流 private InputStream is = null; // 当前的Sheet private int currSheet; // 当前位置 private int currPosition; // Sheet数量 private int numOfSheets; // HSSFWorkbook HSSFWorkbook workbook = null; // 设置Cell之间以空格分割 private static String EXCEL_LINE_DELIMITER = ","; // 设置最大列数 private static int MAX_EXCEL_COLUMNS = 64; // 设置时间格式 private final static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd hhmmss"); // 构造函数创建一个ExcelReader public ExcelReader(FileItem item) throws IOException, Exception { String inputfile = item.getName(); // 判断参数是否为空或没有意义 if (inputfile == null || inputfile.trim().equals("")) { throw new IOException("no input file specified"); } // 取得文件名的后缀名赋值给filetype this.filetype = inputfile.substring(inputfile.lastIndexOf(".") + 1); // 设置开始行为0 currPosition = 0; // 设置当前位置为0 currSheet = 0; // 创建文件输入流 is = item.getInputStream(); // 判断文件格式 if (filetype.equalsIgnoreCase("xls")||filetype.equalsIgnoreCase("xlsx")) { // 如果是Excel文件则创建HSSFWorkbook读取 workbook = new HSSFWorkbook(is); // 设置Sheet数 numOfSheets = workbook.getNumberOfSheets(); } else { throw new Exception("File Type Not Supported"); } } // 函数readLine读取文件的一行 public String readLine() throws IOException { // 如果是XLS文件则通过POI提供的API读取文件 if (filetype.equalsIgnoreCase("xls")) { // 根据currSheet值获得当前的sheet HSSFSheet sheet = workbook.getSheetAt(currSheet); // 判断当前行是否到但前Sheet的结尾 // System.out.println(sheet.getLastRowNum()); if (currPosition > sheet.getLastRowNum()) { // 当前行位置清零 currPosition = 0; // 判断是否还有Sheet if (currSheet != numOfSheets - 1) { currSheet++; // 得到下一张Sheet sheet = workbook.getSheetAt(currSheet); } else { return null; } } // 获取当前行数 int row = currPosition; currPosition++; // 读取当前行数据 return getLine(sheet, row); } return null; } // 函数getLine返回Sheet的一行数据 private String getLine(HSSFSheet sheet, int row) { // 根据行数取得Sheet的一行 HSSFRow rowline = sheet.getRow(row); // 创建字符创缓冲区 StringBuffer buffer = new StringBuffer(); // 获取当前行的列数 int filledColumns; try { filledColumns = rowline.getLastCellNum(); } catch (NullPointerException e) { return null; } HSSFCell cell = null; // 循环遍历所有列 for (int i = 0; i < filledColumns; i++) { // 取得当前Cell cell = rowline.getCell((short) i); String cellvalue = null; if (cell != null) { // 判断当前Cell的Type switch (cell.getCellType()) { // 如果当前Cell的Type为NUMERIC case HSSFCell.CELL_TYPE_NUMERIC: { // 判断当前的cell是否为Date if (HSSFDateUtil.isCellDateFormatted(cell)) { // 如果是Date类型则,取得该Cell的Date值 Date date = cell.getDateCellValue(); // 把Date转换成本地格式的字符串 cellvalue = DATE_FORMAT.format(date); } // 如果是纯数字 else { // 取得当前Cell的数值 Integer num = new Integer((int) cell .getNumericCellValue()); cellvalue = String.valueOf(num); } break; } // 如果当前Cell的Type为STRIN case HSSFCell.CELL_TYPE_STRING: // 取得当前的Cell字符串 cellvalue = cell.getRichStringCellValue().toString() .replaceAll("'", "''"); break; // 默认的Cell值 default: cellvalue = ""; } } else { cellvalue = ""; } // 在每个字段之间插入分割符 if (!"".equals(cellvalue) && cellvalue.trim() != null) { buffer.append(cellvalue).append(EXCEL_LINE_DELIMITER); } } // 以字符串返回该行的数据 return buffer.toString(); } // close函数执行流的关闭操作 public void close() { // 如果is不为空,则关闭InputSteam文件输入流 if (is != null) { try { is.close(); } catch (IOException e) { is = null; } } // 如果reader不为空则关闭BufferedReader文件输入流 if (reader != null) { try { reader.close(); } catch (IOException e) { reader = null; } } } }
public class XSSFExcelReader { // 创建文件输入流 private BufferedReader reader = null; // 文件类型 private String filetype; // 文件二进制输入流 private InputStream is = null; // 当前的Sheet private int currSheet; // 当前位置 private int currPosition; // Sheet数量 private int numOfSheets; // XSSFWorkbook XSSFWorkbook workbook = null; // 设置Cell之间以空格分割 private static String EXCEL_LINE_DELIMITER = ","; // 设置最大列数 private static int MAX_EXCEL_COLUMNS = 64; // 设置时间格式 private final static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd hhmmss"); // 构造函数创建一个ExcelReader public XSSFExcelReader(String inputfile) throws IOException, Exception { // 判断参数是否为空或没有意义 if (inputfile == null || inputfile.trim().equals("")) { throw new IOException("no input file specified"); } // 取得文件名的后缀名赋值给filetype this.filetype = inputfile.substring(inputfile.lastIndexOf(".") + 1); // 设置开始行为0 currPosition = 0; // 设置当前位置为0 currSheet = 0; // 创建文件输入流 is = new FileInputStream(inputfile); // 判断文件格式 if (filetype.equalsIgnoreCase("xlsx") || filetype.equalsIgnoreCase("xlsxx")) { // 如果是Excel文件则创建HSSFWorkbook读取 workbook = new XSSFWorkbook(is); // 设置Sheet数 numOfSheets = workbook.getNumberOfSheets(); } else { throw new Exception("File Type Not Supported"); } } // 函数readLine读取文件的一行 public String readLine() throws IOException { // 如果是XLS文件则通过POI提供的API读取文件 if (filetype.equalsIgnoreCase("xlsx")) { // 根据currSheet值获得当前的sheet XSSFSheet sheet = workbook.getSheetAt(currSheet); // 判断当前行是否到但前Sheet的结尾 // System.out.println(sheet.getLastRowNum()); if (currPosition > sheet.getLastRowNum()) { // 当前行位置清零 currPosition = 0; // 判断是否还有Sheet if (currSheet != numOfSheets - 1) { currSheet++; // 得到下一张Sheet sheet = workbook.getSheetAt(currSheet); } else { return null; } } // 获取当前行数 int row = currPosition; currPosition++; // 读取当前行数据 return getLine(sheet, row); } return null; } // 函数getLine返回Sheet的一行数据 private String getLine(XSSFSheet sheet, int row) { // 根据行数取得Sheet的一行 XSSFRow rowline = sheet.getRow(row); // 创建字符创缓冲区 StringBuffer buffer = new StringBuffer(); // 获取当前行的列数 int filledColumns; try { filledColumns = rowline.getLastCellNum(); } catch (NullPointerException e) { return null; } XSSFCell cell = null; // 循环遍历所有列 for (int i = 0; i < filledColumns; i++) { // 取得当前Cell cell = rowline.getCell((short) i); String cellvalue = null; if (cell != null) { // 判断当前Cell的Type switch (cell.getCellType()) { // 如果当前Cell的Type为NUMERIC case XSSFCell.CELL_TYPE_NUMERIC: { // 判断当前的cell是否为Date XSSFCellStyle style = cell.getCellStyle(); int t = style.getDataFormat(); if (isInternalDateFormat(t)) { // 如果是Date类型则,取得该Cell的Date值 Date date = cell.getDateCellValue(); // 把Date转换成本地格式的字符串 cellvalue = DATE_FORMAT.format(date); } // 如果是纯数字 else { // 取得当前Cell的数值 Integer num = new Integer((int) cell .getNumericCellValue()); cellvalue = String.valueOf(num); } break; } // 如果当前Cell的Type为STRIN case XSSFCell.CELL_TYPE_STRING: // 取得当前的Cell字符串 cellvalue = cell.getRichStringCellValue().toString() .replaceAll("'", "''"); break; // 默认的Cell值 default: cellvalue = ""; } } else { cellvalue = ""; } // 在每个字段之间插入分割符 if (!"".equals(cellvalue) && cellvalue.trim() != null) { buffer.append(cellvalue).append(EXCEL_LINE_DELIMITER); } } // 以字符串返回该行的数据 return buffer.toString(); } /** * given a format ID this will check whether the format represents an * internal date format or not. */ public boolean isInternalDateFormat(int format) { boolean retval = false; switch (format) { // Internal Date Formats as described on page 427 in // Microsoft Excel Dev's Kit... case 0x0e: case 0x0f: case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x2d: case 0x2e: case 0x2f: retval = true; break; default: retval = false; break; } return retval; } // close函数执行流的关闭操作 public void close() { // 如果is不为空,则关闭InputSteam文件输入流 if (is != null) { try { is.close(); } catch (IOException e) { is = null; } } // 如果reader不为空则关闭BufferedReader文件输入流 if (reader != null) { try { reader.close(); } catch (IOException e) { reader = null; } } } }
使用范例
ExcelReader er = new ExcelReader(item); String line = er.readLine(); while (line != null) { count++; sb.append(line).append('\n'); line = er.readLine(); } er.close();
相关推荐
用poi操作excel2007时需要用到的jar包: poi项目的:1.poi-3.9-20121203.jar 2.poi-ooxml-3.9-20121203.jar 3.poi-ooxml-schemas-3.9-20121203.jar ...而操作excel2003时只需要导入poi的poi-3.9-20121203.jar即可。
poi操作excel2007
poi解析excel2003、2007 poi解析excel2003、2007 poi解析excel2003、2007
poi excel poi excel poi excel poi excel poi excel poi excel poi excel poi excel
通过实例代码,帮助大家掌握POI操作excel2003,2007调用的方法名不同,但基本类似。
poi 操作excel模板,读取数据后,讲数据填充到新生成的文件中且提供下载
POI生成Excel POI读取Excel java操作Excel Servlet生成Excel web项目,包含含读取Excel与生成Excel方法
Poi读取Excel2003+Excel2007 Poi生成Excel2003 源码,调用 方法即可
POI操作excel表格 讲的很详细,很有用
java使用poi进行excel2003、excel2007导入,返回List[]>,实测可用 xbean-2.2.0.jar包 与 xmlbeans-2.3.0.jar包冲突 需删除xbean-2.2.0.jar包
NULL 博文链接:https://81365341.iteye.com/blog/1149108
POI操作Excel生成水印,该水印其实就是一个图片,该代码是将文字生成图片,再将图片变成水印的。
java操作excel(支持excel2007), java操作excel(支持excel2007)
poi excel 2003 2007
POI处理 excel2007
为更方便的使用POI的API来操作Excel(2003)文件,对POI中针对Excel文件的读写进行了简单封装。此类中包含以下功能: 1.根据模板创建Excel文件 ...压缩包中包含POI的jar文件、POIExcel的jar文件及POIExcel的源码
通过反射和相应的约定实现POI操作Excel的封装
poi读取excel2007和2003兼容工具例子.利用简单工厂方法
poi 操作 excel 2007 demo 及poi类库
poi操作excel的Demo