![](/img/trans.png)
[英]Out of memory while executing java code to export a DB table data to Excel sheet (.xlsx)
[英]Write data in excel(xlsx) sheet having a table
我正在嘗試在已創建表的excel表(xlsx)中編寫動態數據,基於此我們使用宏在Excel中繪制圖表。
我正在使用POI來編寫數據。
工作表中的表已設置為10行。 當我寫入超過10行的數據時,表格不會擴展。因此,繪制的圖表包含僅對應10行的數據。
如何編寫數據,以便數據始終將表擴展為數據中的行數?
您應該從sheet.createTable();
創建一個XSSFTable
對象sheet.createTable();
。
以下是我在http://thinktibits.blogspot.co.il/2014/09/Excel-Insert-Format-Table-Apache-POI-Example.html中找到的示例 - 只需確保創建的表格涵蓋所有行/列你動態地放在文件中。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;
public class insertTable
{
public static void main(String[] args)
throws FileNotFoundException, IOException
{
/* Read the input file that contains the data to be converted to table */
FileInputStream input_document = new FileInputStream(new File("FormatAsTable.xlsx"));
/* Create Workbook */
XSSFWorkbook my_xlsx_workbook = new XSSFWorkbook(input_document);
/* Read worksheet */
XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0);
/* Create Table into Existing Worksheet */
XSSFTable my_table = sheet.createTable();
/* get CTTable object*/
CTTable cttable = my_table.getCTTable();
/* Define Styles */
CTTableStyleInfo table_style = cttable.addNewTableStyleInfo();
table_style.setName("TableStyleMedium9");
/* Define Style Options */
table_style.setShowColumnStripes(false); //showColumnStripes=0
table_style.setShowRowStripes(true); //showRowStripes=1
/* Define the data range including headers */
AreaReference my_data_range = new AreaReference(new CellReference(0, 0), new CellReference(5, 2));
/* Set Range to the Table */
cttable.setRef(my_data_range.formatAsString());
cttable.setDisplayName("MYTABLE"); /* this is the display name of the table */
cttable.setName("Test"); /* This maps to "displayName" attribute in <table>, OOXML */
cttable.setId(1L); //id attribute against table as long value
/* Add header columns */
CTTableColumns columns = cttable.addNewTableColumns();
columns.setCount(3L); //define number of columns
/* Define Header Information for the Table */
for (int i = 0; i < 3; i++)
{
CTTableColumn column = columns.addNewTableColumn();
column.setName("Column" + i);
column.setId(i+1);
}
/* Write output as File */
FileOutputStream fileOut = new FileOutputStream("Excel_Format_As_Table.xlsx");
my_xlsx_workbook.write(fileOut);
fileOut.close();
}
雖然這個問題有點過時,但我花了4個多小時來解決這個問題,所以我認為發布它可能有所幫助。 這會改變現有表而不是創建新表。
// tell your xssfsheet where its content begins and where it ends
((XSSFWorksheet) sheet).getCTWorksheet().getDimension()
.setRef("A1:H" + (sheet.getLastRowNum() + 1));
CTTable ctTable = sheet.getTables().get(0).getCTTable();
ctTable.setRef("A1:H" + (sheet.getLastRowNum() + 1)); // adjust reference as needed
ctTable.unsetSortState(); // if you had sorted the data in Excel before reading the file,
// you may want an unsorted table in your output file
CTTableColumns ctColumns = ctTable.getTableColumns(); // setting new table columns will
// muck everything up,
// so adjust the existing ones
// remove the old columns first if you plan on expanding your table in the column direction
for (int i = 0; i < ctColumns.getCount(); i++) {
ctColumns.removeTableColumn(0);
}
// throw in your new columns
for (int i = 0; i < tableHeaders.size(); i++) {
CTTableColumn column = ctColumns.addNewTableColumn();
column.setName(tableHeaders.get(i));
column.setId(i + 1);
}
// for some reason this isn't being take care of when columns are modified,
// so fix the column count manually
ctColumns.setCount(tableHeaders.size());
一句警告:如果表中不存在相應的屬性, CTTable
所有unsetX
方法CTTable
將拋出IndexOutOfBoundsException
,因此它們需要是try-catch-ed。 @Gagravarr在評論中提到的另一種方法是首先使用isSetX
,然后就不需要try
。
如果這不起作用,那么你可能在表中設置了一堆其他東西。 不幸的是,低級XML內容(封裝在所有這些CT對象中)的文檔很少,因為它顯然是從規范自動生成的,因此API非常不透明。 我經歷的磨牙過程是弄清楚要使用的是解壓縮.xlsx,查看我需要調整的XML,找出它在我的XSSFSheet中的位置以及我需要如何調整它,然后去尋找合適的CT方法,因為XSSF-API並沒有真正讓你做那么多。 通過這種方式,您可以創建幾乎任何可以在Excel中創建的內容,甚至可以更改一些您不能在那里創建的內容 - 如果您有時間和緊張。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.