Line 0
Link Here
|
|
|
1 |
/* ==================================================================== |
2 |
Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
contributor license agreements. See the NOTICE file distributed with |
4 |
this work for additional information regarding copyright ownership. |
5 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
(the "License"); you may not use this file except in compliance with |
7 |
the License. You may obtain a copy of the License at |
8 |
|
9 |
http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
11 |
Unless required by applicable law or agreed to in writing, software |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
See the License for the specific language governing permissions and |
15 |
limitations under the License. |
16 |
==================================================================== */ |
17 |
|
18 |
package org.apache.poi.xssf.usermodel; |
19 |
|
20 |
import org.apache.poi.ss.usermodel.Cell; |
21 |
import org.apache.poi.xssf.XSSFTestDataSamples; |
22 |
import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
23 |
import org.junit.Test; |
24 |
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable; |
25 |
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn; |
26 |
|
27 |
import java.io.File; |
28 |
import java.io.FileInputStream; |
29 |
import java.io.FileOutputStream; |
30 |
import java.io.IOException; |
31 |
import java.util.ArrayList; |
32 |
import java.util.List; |
33 |
|
34 |
import static org.junit.Assert.assertEquals; |
35 |
import static org.junit.Assert.assertTrue; |
36 |
|
37 |
public final class TestXSSFTable { |
38 |
|
39 |
public TestXSSFTable() { |
40 |
} |
41 |
|
42 |
@Test |
43 |
public void bug56274() throws IOException { |
44 |
// read sample file |
45 |
XSSFWorkbook inputWorkbook = XSSFTestDataSamples.openSampleWorkbook("56274.xlsx"); |
46 |
|
47 |
// read the original sheet header order |
48 |
XSSFRow row = inputWorkbook.getSheetAt(0).getRow(0); |
49 |
List<String> headers = new ArrayList<String>(); |
50 |
for (Cell cell : row) { |
51 |
headers.add(cell.getStringCellValue()); |
52 |
} |
53 |
|
54 |
// save the worksheet as-is using SXSSF |
55 |
File outputFile = File.createTempFile("poi-56274", ".xlsx"); |
56 |
SXSSFWorkbook outputWorkbook = new org.apache.poi.xssf.streaming.SXSSFWorkbook(inputWorkbook); |
57 |
outputWorkbook.write(new FileOutputStream(outputFile)); |
58 |
|
59 |
// re-read the saved file and make sure headers in the xml are in the original order |
60 |
inputWorkbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook(new FileInputStream(outputFile)); |
61 |
CTTable ctTable = inputWorkbook.getSheetAt(0).getTables().get(0).getCTTable(); |
62 |
List<CTTableColumn> ctTableColumnList = ctTable.getTableColumns().getTableColumnList(); |
63 |
|
64 |
assertEquals("number of headers in xml table should match number of header cells in worksheet", |
65 |
headers.size(), ctTableColumnList.size()); |
66 |
for (int i = 0; i < headers.size(); i++) { |
67 |
assertEquals("header name in xml table should match number of header cells in worksheet", |
68 |
headers.get(i), ctTableColumnList.get(i).getName()); |
69 |
} |
70 |
assertTrue(outputFile.delete()); |
71 |
} |
72 |
|
73 |
} |