java poi导出excel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java poi导出excel相关的知识,希望对你有一定的参考价值。
参考技术A用spire.xls.jar也可以导出excel, 代码更简单
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class InsertArray
public static void main(String[] args)
//创建Workbook对象
Workbook wb = new Workbook();
//获取第一张工作表
Worksheet sheet = wb.getWorksheets().get(0);
//定义一维数据
String[] oneDimensionalArray = new String[]"苹果", "梨子", "葡萄", "香蕉";
//将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入
sheet.insertArray(oneDimensionalArray, 1, 1, true);
//定义二维数组
String[][] twoDimensionalArray = new String[][]
"姓名", "年龄", "性别", "学历",
"小张", "25", "男", "本科",
"小王", "24", "男", "本科",
"小李", "26", "女", "本科"
;
//从指定单元格开始写入二维数组到工作表
sheet.insertArray(twoDimensionalArray, 1, 3);
//保存文档
wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);
Java 利用poi 导出excel表格 如何在导出时自由选择路径?
能导出成功,知识代码把路径写死了,想要实现自己选择保存路径。
导出时自由选择路径的代码如下:
1、后台输出Excel文件代码:
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + path);
response.setContentType("Content-Type:application/vnd.ms-excel ");
wb.write(output);
output.close();
2、前端代码:
window.open("getExcelList","_blank");
导出excel数据
* @param id
* @param m
* @return
*/
@RequestMapping("/exportExcel")
public void exportExcel(@RequestParam("id") Integer id, Model m,HttpServletRequest req, HttpServletResponse resp)
try
ExportExcel<P2pLoanPlanVo> ex = new ExportExcel<P2pLoanPlanVo>();
String[] headers = "最迟还款日", "还款金额","剩余几天","逾期几天", "罚息","是否垫付","状态","是否发放收益";
List<P2pLoanPlanVo> dataset = new ArrayList<P2pLoanPlanVo>();
List<P2pLoanPlan> plans = this.planService.getListByLoan(id);
for (int i = 0; i < plans.size(); i++)
P2pLoanPlanVo p2pLoanPlanVo = new P2pLoanPlanVo();
p2pLoanPlanVo.setRepayDate(plans.get(i).getRepayDate());
p2pLoanPlanVo.setRepayAmount(plans.get(i).getRepayAmount());
if(plans.get(i).getRepayDays() >= 0 && plans.get(i).getStatus() == 0)
p2pLoanPlanVo.setRepayDays(plans.get(i).getRepayDays());
else
p2pLoanPlanVo.setRepayDays(0);
if(plans.get(i).getRepayDays() < 0 && plans.get(i).getStatus() == 0)
p2pLoanPlanVo.setRepayYqDays(-plans.get(i).getRepayDays());
else
p2pLoanPlanVo.setRepayYqDays(0);
参考技术A 主要代码如下:后台输出Excel文件代码:
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + path);
response.setContentType("Content-Type:application/vnd.ms-excel ");
wb.write(output);
output.close();
前端代码:window.open("getExcelList","_blank"); 参考技术B 做个临时文件,在重定向到这个临时文件,浏览器就会选择下载追问
我不明白,没学这个多久,望耐心解释~十分感谢~
追答在servlet中,
//把上边的改成try
wb.write(response.getOutputStream());
参考技术C 可以用pageoffice导出excel. pageoffice 调用的是本地的office,导出excel可以弹出另存的对话框,用户可以选择自己想要的位置进行保存.
以上是关于java poi导出excel的主要内容,如果未能解决你的问题,请参考以下文章
Java 利用poi 导出excel表格 如何在导出时自由选择路径?
急!!!java用poi导出excel文件,打开导出的文件时报错“文件错误,数据可能丢失”