2012-12-12 37 views
0

我有現有的Java代碼,將抓住應用程序中生成的表並將其導出到Excel電子表格,但現在它正在寫入Excel電子表格,沒有任何格式。我希望能夠添加到Java代碼來擴展每列(如雙擊Excel中的列)以適應文本。我如何實現這一目標?Java代碼擴大每一列以適應文本(自動調整功能)

繼承人寫入到Excel電子表格

/** 
* This method writes data to the row. 
* 
* @param header The header row for the data to be written. 
* 
* @param data  The data rows to be written. 
* 
* @param rowIndex The starting index for the row. 
* 
* @param columnIndex The starting index for the column. 
* 
* @throws IOException The file cannot be written to. 
* 
* @throws FileNotFoundException The file cannot be found or open. 
*/ 

private HSSFSheet dataSheet; 
private int processDataToSheet(List<Excel_o> header, List<List<Excel_o>> data, int rowIndex, int columnIndex) 
        throws IOException, FileNotFoundException 
{ 
    //process header data 
    processRowData(dataSheet, rowIndex, header, columnIndex); 

    //write data to sheet 
    for(List<Excel_o> singleList: data){ 
     //process data for the whole row. 
     processRowData(dataSheet, ++rowIndex, singleList, columnIndex); 
    } 

    return rowIndex; 
} 

我擡頭可供apachi.poi.hssf.usermodel.HSSFSheet(在我的代碼數據表變量)方法的代碼的一部分,它說它有一個名爲autoSizeColumn的方法,但是當我查看Eclipse中的下拉列表時,沒有可用的選項。有這個原因嗎?

+0

那你到底要?請展示一些你已經出現的東西。 – Ravi

+0

您是否使用Apache POI在Java中生成Excels? –

+0

是的即時通訊使用Apache poi生成Excels – user974047

回答

1

您可以使用autoSizeColumn()方法,見API參考here

+0

問題是,由於某種原因,它不是我在數據表中鍵入後下拉列表中的選項。 – user974047

+0

您是否確定您使用的是足夠新的Apache POI副本?我強烈建議你嘗試升級到POI 3.9並嘗試使用,那麼答案應該很好! – Gagravarr

相關問題