提交 4d588d16 作者: yanxin

处理excel单元格长度问题

上级 e946419c
package org.apache.poi.ss;
import org.apache.poi.ss.util.CellReference;
/**
* @author : wp
* @title : SpreadsheetVersion
* @description: 重写SpreadsheetVersion
* @date : Created in 2022/6/7 19:57
* @modified By:
*/
public enum SpreadsheetVersion {
/**
* Excel97 format aka BIFF8
* <ul>
* <li>The total number of available rows is 64k (2^16)</li>
* <li>The total number of available columns is 256 (2^8)</li>
* <li>The maximum number of arguments to a function is 30</li>
* <li>Number of conditional format conditions on a cell is 3</li>
* <li>Number of cell styles is 4000</li>
* <li>Length of text cell contents is 32767</li>
* </ul>
*/
EXCEL97(0x10000, 0x0100, 30, 3, 4000, Integer.MAX_VALUE),
/**
* Excel2007
*
* <ul>
* <li>The total number of available rows is 1M (2^20)</li>
* <li>The total number of available columns is 16K (2^14)</li>
* <li>The maximum number of arguments to a function is 255</li>
* <li>Number of conditional format conditions on a cell is unlimited
* (actually limited by available memory in Excel)</li>
* <li>Number of cell styles is 64000</li>
* <li>Length of text cell contents is 32767</li>
* <ul>
*/
EXCEL2007(0x100000, 0x4000, 255, Integer.MAX_VALUE, 64000, Integer.MAX_VALUE);
private final int _maxRows;
private final int _maxColumns;
private final int _maxFunctionArgs;
private final int _maxCondFormats;
private final int _maxCellStyles;
private final int _maxTextLength;
private SpreadsheetVersion(int maxRows, int maxColumns, int maxFunctionArgs, int maxCondFormats, int maxCellStyles, int maxText) {
_maxRows = maxRows;
_maxColumns = maxColumns;
_maxFunctionArgs = maxFunctionArgs;
_maxCondFormats = maxCondFormats;
_maxCellStyles = maxCellStyles;
_maxTextLength = maxText;
}
/**
* @return the maximum number of usable rows in each spreadsheet
*/
public int getMaxRows() {
return _maxRows;
}
/**
* @return the last (maximum) valid row index, equals to <code> getMaxRows() - 1 </code>
*/
public int getLastRowIndex() {
return _maxRows - 1;
}
/**
* @return the maximum number of usable columns in each spreadsheet
*/
public int getMaxColumns() {
return _maxColumns;
}
/**
* @return the last (maximum) valid column index, equals to <code> getMaxColumns() - 1 </code>
*/
public int getLastColumnIndex() {
return _maxColumns - 1;
}
/**
* @return the maximum number arguments that can be passed to a multi-arg function (e.g. COUNTIF)
*/
public int getMaxFunctionArgs() {
return _maxFunctionArgs;
}
/**
* @return the maximum number of conditional format conditions on a cell
*/
public int getMaxConditionalFormats() {
return _maxCondFormats;
}
/**
* @return the maximum number of cell styles per spreadsheet
*/
public int getMaxCellStyles() {
return _maxCellStyles;
}
/**
*
* @return the last valid column index in a ALPHA-26 representation
* (<code>IV</code> or <code>XFD</code>).
*/
public String getLastColumnName() {
return CellReference.convertNumToColString(getLastColumnIndex());
}
/**
* @return the maximum length of a text cell
*/
public int getMaxTextLength() {
return _maxTextLength;
}
}
package org.apache.poi.xwpf.usermodel;
import lombok.Data;
import java.util.List;
@Data
public class DocEntity {
public String name;
public String content;
public List<DocEntity> section;
public String parentId;
public String id;
}
package org.apache.poi.xwpf.usermodel;
import lombok.Data;
import java.util.List;
@Data
public class DocResult {
String content;
List<DocEntity> chapter;
}
package org.apache.poi.xwpf.usermodel;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFSDT;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UXWPFDocument extends XWPFDocument {
public UXWPFDocument(InputStream inputStream) throws IOException {
super(inputStream);
}
public List<XWPFSDT> getContentControls()
{
return this.contentControls;
}
}
package org.apache.poi.xwpf.usermodel;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.util.ArrayList;
import java.util.List;
public class XWPFSDTContent implements ISDTContent {
public List<ISDTContents> getBodyElements() {
return bodyElements;
}
public void setBodyElements(List<ISDTContents> bodyElements) {
this.bodyElements = bodyElements;
}
// private final IBody part;
// private final XWPFDocument document;
// private List<XWPFParagraph> paragraphs = new ArrayList<>();
// private List<XWPFTable> tables = new ArrayList<>();
// private List<XWPFRun> runs = new ArrayList<>();
// private List<XWPFSDT> contentControls = new ArrayList<>();
private List<ISDTContents> bodyElements = new ArrayList<>();
public XWPFSDTContent(CTSdtContentRun sdtRun, IBody part, IRunBody parent) {
if (sdtRun == null) {
return;
}
for (CTR ctr : sdtRun.getRArray()) {
XWPFRun run = new XWPFRun(ctr, parent);
// runs.add(run);
bodyElements.add(run);
}
}
public XWPFSDTContent(CTSdtContentBlock block, IBody part, IRunBody parent) {
if (block == null) {
return;
}
XmlCursor cursor = block.newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, part);
bodyElements.add(p);
// paragraphs.add(p);
} else if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, part);
bodyElements.add(t);
// tables.add(t);
} else if (o instanceof CTSdtBlock) {
XWPFSDT c = new XWPFSDT(((CTSdtBlock) o), part);
bodyElements.add(c);
// contentControls.add(c);
} else if (o instanceof CTR) {
XWPFRun run = new XWPFRun((CTR) o, parent);
// runs.add(run);
bodyElements.add(run);
}
}
cursor.dispose();
}
@Override
public String getText() {
StringBuilder text = new StringBuilder();
boolean addNewLine = false;
for (int i = 0; i < bodyElements.size(); i++) {
Object o = bodyElements.get(i);
if (o instanceof XWPFParagraph) {
appendParagraph((XWPFParagraph) o, text);
addNewLine = true;
} else if (o instanceof XWPFTable) {
appendTable((XWPFTable) o, text);
addNewLine = true;
} else if (o instanceof XWPFSDT) {
text.append(((XWPFSDT) o).getContent().getText());
addNewLine = true;
} else if (o instanceof XWPFRun) {
text.append(o);
addNewLine = false;
}
if (addNewLine && i < bodyElements.size() - 1) {
text.append("\n");
}
}
return text.toString();
}
private void appendTable(XWPFTable table, StringBuilder text) {
//this works recursively to pull embedded tables from within cells
for (XWPFTableRow row : table.getRows()) {
List<ICell> cells = row.getTableICells();
for (int i = 0; i < cells.size(); i++) {
ICell cell = cells.get(i);
if (cell instanceof XWPFTableCell) {
text.append(((XWPFTableCell) cell).getTextRecursively());
} else if (cell instanceof XWPFSDTCell) {
text.append(((XWPFSDTCell) cell).getContent().getText());
}
if (i < cells.size() - 1) {
text.append("\t");
}
}
text.append('\n');
}
}
private void appendParagraph(XWPFParagraph paragraph, StringBuilder text) {
for (IRunElement run : paragraph.getRuns()) {
text.append(run);
}
}
@Override
public String toString() {
return getText();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论