java如何导入文件怎么打开_如何在java中用文本文件导入数据

展开全部

//导出

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

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值