实际上我尝试使用java在HSSFCell中存储一些数据,但是我收到了类似
的错误java.lang.IllegalArgumentException: The maximum length of cell contents (text) i
s 32,767 characters
at org.apache.poi.hssf.usermodel.HSSFCell.setCellValue(HSSFCell.java:559
)
at org.apache.poi.hssf.usermodel.HSSFCell.setCellValue(HSSFCell.java:533
)
at application.ExtractUI.datatoexcel(ExtractUI.java:272)
at application.ExtractUI$3.getData(ExtractUI.java:208)
at application.ExtractUI$3.handle(ExtractUI.java:198)
at application.ExtractUI$3.handle(ExtractUI.java:1)
任何人都可以建议我增加细胞长度的方法,即超过32767个字符???
我使用了以下代码,我得到了上述错误
public void datatoexcel(ResultSet rs) {
try {
int iter = 0;
ResultSetMetaData rmeta = rs.getMetaData();
int col = rmeta.getColumnCount();
HSSFWorkbook workbook = new HSSFWorkbook();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy HHmmss");
String pa = pth + "\\" + sdf.format(date) + ".xlsx";
System.out.println(pa);
FileOutputStream out = new FileOutputStream(new File(pa));
HSSFSheet sheet = workbook.createSheet();
HSSFRow myRow = null;
HSSFCell myCell = null;
// Font style for headers
HSSFFont boldFont = workbook.createFont();
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
boldFont.setColor(HSSFFont.COLOR_RED);
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(boldFont);
while (rs.next()) {
// limit the data to 1000 anad create a new sheet
if (iter == 1000) {
sheet = workbook.createSheet();
iter = 0;
}
// Adding header to the first row
if (iter == 0) {
myRow = sheet.createRow(iter);
for (int k = 1, j = 0; k <= col && j < col; k++) {
myCell = myRow.createCell( j);
myCell.setCellValue(rmeta.getColumnName(k));
// set style to font
myCell.setCellStyle(cellStyle);
j++;
}
iter++;
}
// Adding data from 2nd Row
myRow = sheet.createRow(iter);
for (int k = 1, j = 0; k <= col && j < col; k++) {
myRow.createCell( j).setCellValue(
rs.getString(rmeta.getColumnName(k)));
j++;
}
iter++;
}
workbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
任何建议??
答案 0 :(得分:7)
您唯一的选择是切换文件格式。 .xls
和.xlsx
文件格式的严重限制为32,767个字符。 Apache POI只是强制执行文件格式+ Excel限制。您可以在Microsoft文档中查看这些限制的详细信息,也可以在this Apache POI javadoc page
如果您确实需要长文本,则需要切换到其他文件格式,例如CSV
答案 1 :(得分:-1)
private void writeIssueDataForEachRow(Issue issue, Row row, CellStyle style,
List<ColumnIndex> customFieldDefinitions) {
Cell cell = row.createCell(0);
cell.setCellValue(issue.getId()); // 編號
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(issue.getSubject()); // 主旨
cell.setCellStyle(style);
// substring 的原因是要避開 The maximum length of cell contents (text) is 32,767 characters
cell = row.createCell(2);
cell.setCellValue(StringUtils.substring(issue.getDescription(), 0, 32767)); // 敘述
cell.setCellStyle(style);
}