liferay 使用poi對excel文檔進行復制後賦值,然後下載

在 JSP 頁面 提交方式好像不能用ajax,所以使用

function <portlet:namespace/>downloadaaaa(){

window.open('<%=downloadURL%>');

}

在控制層

@ActionMapping(value = "download")
public void download(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, PortletException, EncryptedDocumentException, InvalidFormatException, URISyntaxException{
String orderId=ParamUtil.getString(actionRequest, "orderid");
Demo demo=demoService.get(Integer.parseInt(orderId));
String portletPath = SystemProperties.get("liferay.home");
String tPath = portletPath+"/data/";
WriteData w = new WriteData();
String path = w.writeDataToExcel(tPath, "t.xls", demo);
File file = new File(path);
ServletResponseUtil.sendFile(PortalUtil.getHttpServletResponse(actionResponse), file.getName(), new FileInputStream(file), ContentTypes.APPLICATION_VND_MS_EXCEL);
w.deleteFile(path);
}


在Java代碼中

package cn.gewut.portal.portlet;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


import cn.gewut.portal.model.Demo;


public class WriteData {
/*public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
writeDataToExcel("E:/","t.xls",null);
}*/

/*
* tPath 模板路徑
* tName 模板名稱
* demo  實體

*/
public   String writeDataToExcel(String tPath,String tName,Demo demo) throws EncryptedDocumentException, InvalidFormatException, IOException{
String oldPath = tPath+tName;
long sTime = System.currentTimeMillis();
String newPath = tPath+String.valueOf(sTime)+".xls";
copyFile(oldPath, newPath);
write(newPath,demo);


return newPath;
}
public  boolean deleteFile(String fileName) {  
 File file = new File(fileName);  
 // 如果文件路徑所對應的文件存在,並且是一個文件,則直接刪除  
 if (file.exists() && file.isFile()) {  
  if (file.delete()) {  
   System.out.println("刪除單個文件" + fileName + "成功!");  
   return true;  
  } else {  
   System.out.println("刪除單個文件" + fileName + "失敗!");  
   return false;  
  }  
 } else {  
  System.out.println("刪除單個文件失敗:" + fileName + "不存在!");  
  return false;  
 }  


public  void write(String newPath,Demo demo) throws EncryptedDocumentException, InvalidFormatException, IOException{
FileInputStream is = new FileInputStream(newPath); //文件流  
    Workbook workbook = WorkbookFactory.create(is); //這種方式 Excel 2003/2007/2010 都是可以處理的 
    Sheet sheet = workbook.getSheetAt(0);
    Row row = null;
    Cell cell = null;
    row = sheet.getRow(3);
    //儀器編號
    cell = row.getCell(1);
    cell.setCellValue(demo.getEquipmentnumber());
    
    //儀器名稱
    cell = row.getCell(3);
    cell.setCellValue(demo.getEquipmentname());
    
     
    
 // 文件流
  File file = new File(newPath);
  OutputStream os = new FileOutputStream(file);
  os.flush();
  workbook.write(os);
  os.close();
  is.close();
}


/*
* 複製文件

*/
public   void copyFile(String oldPath, String newPath) { 
try { 
int bytesum = 0; 
int byteread = 0; 
File oldfile = new File(oldPath); 
if (oldfile.exists()) { //文件存在時 
InputStream inStream = new FileInputStream(oldPath); //讀入原文件 
FileOutputStream fs = new FileOutputStream(newPath); 
byte[] buffer = new byte[1444]; 
int length; 
while ( (byteread = inStream.read(buffer)) != -1) { 
bytesum += byteread; //字節數 文件大小 
fs.write(buffer, 0, byteread); 

inStream.close();
fs.close();


catch (Exception e) { 
System.out.println("複製單個文件操作出錯"); 
e.printStackTrace(); 






}



以上有幾點需要注意:

1.在對cell操作完成後要使用流把workbook  flush 輸出到對應的excel中

2.使用完流後要對流進行關閉,否則後面對該文檔刪除的時候刪除不了。

3.(個人感覺) 好像不能使用ajax提交


新人,如果不對,請留言告訴我。謝謝


發佈了43 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章