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提交


新人,如果不对,请留言告诉我。谢谢


你可能感兴趣的:(poi,Excel,liferay)