展开全部
//导出
public void budgetlistExp(HttpServletRequest request,HttpServletResponse response) throws BiffException, IOException, RowsExceededException, WriteException{
List lists = (List)request.getSession().getAttribute("budgetlist");//得到session中保存的集合
System.out.println(lists.size());//打印一下数量
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");//格式化时间
String temp = request.getRealPath("")+"\\files\\储备库e68a8462616964757a686964616f31333330343864.xls";//得到模板地址
String outPath = request.getRealPath("")+"\\files\\outfile.xls";//缓存地址
File file = new File(outPath);//创建文件
if(file.exists()){//如果文件存在删除
file.delete();
}
FileOutputStream out = new FileOutputStream(outPath);//输出流
Workbook wb = Workbook.getWorkbook(new File(temp));//创建工作簿(temp新文件)
WritableWorkbook workbook = Workbook.createWorkbook(out,wb);
WritableSheet sheet = workbook.getSheet(0);//得到当前第一个sheet
//循环操作list集合
for(int i=0;i
Budgetapply budgetapply = lists.get(i);//得到该对象
Label label = new Label(0,i+1,budgetapply.getItem().getItemname());//项目名称第0列,第i+1行,内容,
sheet.addCell(label);
label = new Label(1,i+1,budgetapply.getExplain());//列支说明
sheet.addCell(label);
label = new Label(2,i+1,budgetapply.getApplyamount()+"");//申报金额
sheet.addCell(label);
label = new Label(3,i+1,budgetapply.getActualamount()+"");//下达金额
sheet.addCell(label);
label = new Label(4,i+1,budgetapply.getSubject().getSubjectcode()+"");//第4列,第i+1行,内容,科目号
sheet.addCell(label);//添加到单元格
label = new Label(5,i+1,budgetapply.getSubject().getSubjectname());//科目名称
sheet.addCell(label);
label = new Label(6,i+1,budgetapply.getDepartment().getDepartmentname());//申报部门
sheet.addCell(label);
label = new Label(7,i+1,budgetapply.getSetupdate()+"");//申报时间
sheet.addCell(label);
label = new Label(8,i+1,budgetapply.getReviewdate()+"");//审批时间
sheet.addCell(label);
label = new Label(9,i+1,budgetapply.getUser().getUsername());//操作人
sheet.addCell(label);
}
workbook.write();//写入temp
workbook.close();//关闭工作薄
out.close();//关闭流
String outname="储备库.xls";//工作薄取名
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;" + " filename="+new String(outname.getBytes(), "ISO-8859-1"));
File file1 = new File(outPath);//新建一个文件
FileInputStream inputStream = new FileInputStream(file1);//输出流输出
OutputStream os=response.getOutputStream();
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
os.write(buf, 0, length);
}
inputStream.close();
os.flush();
}
//导入
/**
* 文件上传
* @param request
*/
public String upload(HttpServletRequest request) {
String time = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());//获取当前时间
String uploadPath = request.getRealPath("")+"\\files"; // 上传文件的目录
String tempPath = request.getRealPath("")+"\\files\\temp"; // 临时文件目录
String filePath="";
File tempPathFile;
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(41943040); // 设置最大文件尺寸,这里是40MB
List items = upload.parseRequest(request);// 得到所有的文件
Iterator i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
filePath=time+LzgUtil.getSuffixName(fullFile.getName());//重组的文件名 时间+后缀名 防止重复
File savedFile = new File(uploadPath,filePath);
fi.write(savedFile);
}
}
} catch (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
return uploadPath+"\\"+filePath;
}
这里用的jar包是jxl
已赞过
已踩过<
你对这个回答的评价是?
评论
收起