首页 > 社交 > 科普中国

POI导入导出百万级数据Excel

常驻编辑 科普中国 2022-05-17 表头   空行   级数   扩展名   字符串   标识   索引   类型   文件   内容   数据
< r.length(); ++c) { if (Character.isDigit(r.charAt(c))) { firstDigit = c; break; } } curCol = nameToColumn(r.substring(0, firstDigit));//获取当前读取的列数 //设定单元格类型 this.setNextDataType(attributes); } //当元素为t时 if ("t".equals(name)) { isTElement = true; } else { isTElement = false; } //置空 lastIndex = ""; } /** * 第二个执行 * 得到单元格对应的索引值或是内容值 * 如果单元格类型是字符串、INLINESTR、数字、日期,lastIndex则是索引值 * 如果单元格类型是布尔值、错误、公式,lastIndex则是内容值 * * @param ch * @param start * @param length * @throws SAXException */ @Override public void characters(char[] ch, int start, int length) throws SAXException { lastIndex += new String(ch, start, length); } /** * 第三个执行 * * @param uri * @param localName * @param name * @throws SAXException */ @Override public void endElement(String uri, String localName, String name) throws SAXException { //t元素也包含字符串 if (isTElement) {//这个程序没经过 //将单元格内容加入rowlist中,在这之前先去掉字符串前后的空白符 String value = lastIndex.trim(); cellList.add(curCol, value.replaceAll(" ", "")); isTElement = false; //如果里面某个单元格含有值,则标识该行不为空行 if (value != null && !"".equals(value)) { flag = true; } } else if ("v".equals(name)) { //v => 单元格的值,如果单元格是字符串,则v标签的值为该字符串在SST中的索引 String value = this.getDataValue(lastIndex.trim(), "");//根据索引值获取对应的单元格值 //补全单元格之间的空单元格 if (curCol - lastCurCol > 1) { curflag = true; } for (int i = lastCurCol; i < curCol; ++i) { if (curflag && i > lastCurCol) { cellList.add(i, ""); } } if (curCol > -1) { lastCurCol = curCol; } cellList.add(curCol, value.replaceAll(" ", "")); //如果里面某个单元格含有值,则标识该行不为空行 if (value != null && !"".equals(value.trim())) { flag = true; } } else { //如果标签名称为row,这说明已到行尾,调用optRows()方法 if ("row".equals(name)) { //默认第一行为表头,以该行单元格数目为最大数目 if (curRow == headerIncex) { sendHeaderRows(filePath, sheetName, sheetIndex, curRow, cellList); maxcurCol = cellList.size() - 1; } //补全一行尾部可能缺失的单元格 if (maxcurCol > lastCurCol) { curflag = true; } for (int i = lastCurCol; i < maxcurCol; ++i) { if (curflag && flag) { cellList.add(i, ""); } } if (flag && curRow >= dataIndex) { //该行不为空行且该行不是第一行,则发送(第一行为列名,不需要) sendDataRows(filePath, sheetName, sheetIndex, curRow, cellList); totalRows++; } cellList.clear(); curRow++; curCol = 0; lastCurCol = -1; flag = false; } } } /** * 处理数据类型 * * @param attributes */ public void setNextDataType(Attributes attributes) { nextDataType = CellDataType.NUMBER; //cellType为空,则表示该单元格类型为数字 formatIndex = -1; formatString = null; String cellType = attributes.getValue("t"); //单元格类型 String cellStyleStr = attributes.getValue("s"); // //String columnData = attributes.getValue("r"); //获取单元格的位置,如A1,B1 if ("b".equals(cellType)) { //处理布尔值 nextDataType = CellDataType.BOOL; } else if ("e".equals(cellType)) { //处理错误 nextDataType = CellDataType.ERROR; } else if ("inlineStr".equals(cellType)) { nextDataType = CellDataType.INLINESTR; } else if ("s".equals(cellType)) { //处理字符串 nextDataType = CellDataType.SSTINDEX; } else if ("str".equals(cellType)) { nextDataType = CellDataType.FORMULA; } if (cellStyleStr != null) { //处理日期 int styleIndex = Integer.parseInt(cellStyleStr); XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); formatIndex = style.getDataFormat(); formatString = style.getDataFormatString(); if (formatString == null) { nextDataType = CellDataType.NULL; formatString = BuiltinFormats.getBuiltinFormat(formatIndex); } else if (formatString.contains("m/d/yy")) { nextDataType = CellDataType.DATE; formatString = "yyyy-MM-dd hh:mm:ss"; } } } /** * 对解析出来的数据进行类型处理 * * @param value 单元格的值, * value代表解析:BOOL的为0或1, ERROR的为内容值,FORMULA的为内容值,INLINESTR的为索引值需转换为内容值, * SSTINDEX的为索引值需转换为内容值, NUMBER为内容值,DATE为内容值 * @param thisStr 一个空字符串 * @return */ public String getDataValue(String value, String thisStr) { switch (nextDataType) { // 这几个的顺序不能随便交换,交换了很可能会导致数据错误 case BOOL: //布尔值 char first = value.charAt(0); thisStr = first == '0' ? "FALSE" : "TRUE"; break; case ERROR: //错误 thisStr = ""ERROR:" + value.toString() + '"'; break; case FORMULA: //公式 thisStr = '"' + value.toString() + '"'; break; case INLINESTR: XSSFRichTextString rtsi = new XSSFRichTextString(value.toString()); thisStr = rtsi.toString(); rtsi = null; break; case SSTINDEX: //字符串 String sstIndex = value.toString(); try { int idx = Integer.parseInt(sstIndex); XSSFRichTextString rtss = new XSSFRichTextString(sst.getEntryAt(idx));//根据idx索引值获取内容值 thisStr = rtss.toString(); rtss = null; } catch (NumberFormatException ex) { thisStr = value.toString(); } break; case NUMBER: //数字 if (formatString != null) { thisStr = formatter.formatRawCellContents(Double.parseDouble(value), formatIndex, formatString).trim(); } else { thisStr = value; } thisStr = thisStr.replace("_", "").trim(); break; case DATE: //日期 thisStr = formatter.formatRawCellContents(Double.parseDouble(value), formatIndex, formatString); // 对日期字符串作特殊处理,去掉T thisStr = thisStr.replace("T", " "); break; default: thisStr = " "; break; } return thisStr; } public int countNullCell(String ref, String preRef) { //excel2007最大行数是1048576,最大列数是16384,最后一列列名是XFD String xfd = ref.replaceAll("d+", ""); String xfd_1 = preRef.replaceAll("d+", ""); xfd = fillChar(xfd, 3, '@', true); xfd_1 = fillChar(xfd_1, 3, '@', true); char[] letter = xfd.toCharArray(); char[] letter_1 = xfd_1.toCharArray(); int res = (letter[0] - letter_1[0]) * 26 * 26 + (letter[1] - letter_1[1]) * 26 + (letter[2] - letter_1[2]); return res - 1; } public String fillChar(String str, int len, char let, boolean isPre) { int len_1 = str.length(); if (len_1

相关阅读:

  • 如何打印表头(打印表格表头怎么每页都有)
  • 有了这个开源工具后,我五点就下班了
  • 行动
  • 加拿大枫叶卡续签、补发、补发—IMM5444表填写指南
  • Linux
  • 如何输入字符串(python如何输入字符串)
  • 如何设置条件格式(如何输入数字字符串)
  • 「数据结构之字典树Trie」C语言版本实现
  • #汇编语言#课程设计1#王爽著
  • JavaScript中的数据类型判断
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。