allow xlsx files to have more columns (#2602)

This commit is contained in:
PJ Fanning 2020-04-26 17:07:26 +02:00 committed by GitHub
parent fe7fcce94b
commit ab64303cbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View File

@ -42,6 +42,7 @@ import java.util.Properties;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
@ -98,10 +99,12 @@ public class XlsExporter implements StreamExporter {
@Override
public void addRow(List<CellData> cells, boolean isHeader) {
Row r = s.createRow(rowCount++);
int maxColumns = getSpreadsheetVersion().getMaxColumns();
int maxTextLength = getSpreadsheetVersion().getMaxTextLength();
for (int i = 0; i < cells.size(); i++) {
Cell c = r.createCell(i);
if (i == 255 && cells.size() > 256) {
if (i == (maxColumns - 1) && cells.size() > maxColumns) {
c.setCellValue("ERROR: TOO MANY COLUMNS");
break;
} else {
@ -119,9 +122,9 @@ public class XlsExporter implements StreamExporter {
c.setCellStyle(dateStyle);
} else {
String s = cellData.text;
if (s.length() > 32767) {
if (s.length() > maxTextLength) {
// The maximum length of cell contents (text) is 32,767 characters
s = s.substring(0, 32767);
s = s.substring(0, maxTextLength);
}
c.setCellValue(s);
}
@ -151,4 +154,11 @@ public class XlsExporter implements StreamExporter {
wb.close();
}
/**
* @return POI <code></code>SpreadsheetVersion</code> with metadata about row and column limits
*/
SpreadsheetVersion getSpreadsheetVersion() {
return xml ? SpreadsheetVersion.EXCEL2007 : SpreadsheetVersion.EXCEL97;
}
}

View File

@ -45,6 +45,7 @@ import java.time.OffsetDateTime;
import java.util.Properties;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.SpreadsheetVersion;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
@ -113,6 +114,12 @@ public class XlsExporterTests extends RefineTest {
Assert.assertEquals(SUT.getContentType(), "application/vnd.ms-excel");
}
@Test
public void getSpreadsheetVersion(){
XlsExporter exporter = (XlsExporter)SUT;
Assert.assertEquals(exporter.getSpreadsheetVersion(), SpreadsheetVersion.EXCEL97);
}
@Test
public void exportSimpleXls() throws IOException {
CreateGrid(2, 2);

View File

@ -34,6 +34,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.exporters;
import java.io.ByteArrayInputStream;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
@ -115,6 +117,12 @@ public class XlsxExporterTests extends RefineTest {
Assert.assertEquals(SUT.getContentType(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
@Test
public void getSpreadsheetVersion(){
XlsExporter exporter = (XlsExporter)SUT;
Assert.assertEquals(exporter.getSpreadsheetVersion(), SpreadsheetVersion.EXCEL2007);
}
@Test
public void exportSimpleXlsx(){
CreateGrid(2, 2);