2015-09-05 47 views
0

我需要將xlsx文件上傳到本地文件夾,並且我需要以網格格式顯示xlsx文件,因此我已將文件上載到本地文件夾,但我需要代碼以顯示xlsx文件以網格格式。用於在網格中顯示xlsx文件內容的j​​ava代碼

我的代碼是:

ist<FileItem> multiparts = upload.parseRequest(request); 

for (FileItem item : multiparts) { 
    if (!item.isFormField()) { 
     String name = new File(item.getName()).getName(); 
     System.out.println("name::::"+name); 
     item.write(new File(UPLOAD_DIRECTORY + File.separator + name)); 
    } 
} 
+1

您可以用'阿帕奇poi'閱讀是'xls'或'xlsx' – Srinu

回答

0

使用Apache POI您可以讀取和寫入MS Excel文件。它能夠處理XLSXLSX格式的電子表格。您可以使用公式,字體,圖像,樣式等。另外,您可以讀寫MS WordMS PowerPoint文件。

Here is the example of Apache POI.

1

下面存在.xlsx文件寫一個類。注意要運行這個文件,你需要下載Apache poi lib並在你的項目中添加所有的.jar文件。 在代碼中,我正在寫一個帶有A到Z字符和ASCII值的.xlsx文件。我也在代碼中寫了註釋,以便理解代碼,如果您仍有疑問,請發表評論。

package com; 
    import java.io.File; 
    import java.io.FileOutputStream; 
    import org.apache.poi.ss.usermodel.Cell; 
    import org.apache.poi.xssf.usermodel.XSSFRow; 
    import org.apache.poi.xssf.usermodel.XSSFSheet; 
    import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
    public class WriteSheet 
    { 
     public static void main(String[] args) throws Exception 
     { 
      //Creating a blank workbook 
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      //Creating a blank sheet 
      XSSFSheet spreadsheet = workbook.createSheet( 
      "ASCII Values from A to Z");  
      int j=0; 
      int ascii=65; 
      for(int i=0; i<26; i++) 
      { 
       //Creating row object 
       XSSFRow row= spreadsheet.createRow(i); 
       //Creating a cell 
       Cell cell = row.createCell(j); 
       j++; 
       //Adding values to cell 
       cell.setCellValue(""+ascii); 
       //Creating a new cell in same row 
       cell = row.createCell(j); 
       //Adding values to new cell 
       cell.setCellValue(""+(char)ascii); 
       ascii++; 
       j=0;   
      } 
      FileOutputStream out = new FileOutputStream( 
      new File("C:\\Users\\TANAY\\Desktop\\Writesheet.xlsx")); 
      workbook.write(out); 
      out.close(); 
      System.out.println( 
      "Writesheet.xlsx written successfully"); 
      workbook.close(); 
     } 
    } 
0

在下面類我從.xlsx文件讀取和顯示數據,我分別寫這篇所以會以書面的.xlsx和閱讀.xlsx文件沒有混亂。注意運行這個文件,你需要下載Apache poi lib並在項目中添加所有的.jar文件。我也寫在代碼中的註釋,這樣就很容易理解的代碼,如果你還有疑問,請評論

package com; 
import java.io.File; 
import java.io.FileInputStream; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
public class ReadSheet 
{ 
    static XSSFRow row; 
    public static void main(String[] args) throws Exception 
    { 
     //File to be read 
     FileInputStream fis = new FileInputStream(new File("C:\\Users\\TANAY\\Desktop\\Writesheet.xlsx")); 
     //Creating workbook object 
     XSSFWorkbook workbook = new XSSFWorkbook(fis); 
     //getting a spreadsheet 
     XSSFSheet spreadsheet = workbook.getSheetAt(0); 
     //to get last row number in spreadsheet 
     int rowcount=spreadsheet.getLastRowNum(); 
     int j=0; 
     for (int i=0; i<=rowcount; i++) 
     { 
     //getting a row 
     row= spreadsheet.getRow(i); 
     //getting a cell 
     Cell cel= row.getCell(j); 
     //printing value of cell 
     System.out.print(cel.getStringCellValue()); 
     j++; 
     //getting next cell from same row 
     cel=row.getCell(j); 
     //displaying cell values 
     System.out.print(" "+cel.getStringCellValue()); 
     j=0; 
     System.out.println(); 
     } 
    fis.close(); 
    workbook.close(); 
    } 
}