java 导出excel

简介:

直入主题。上代码。

//导出excel
@RequestMapping(value="excel")
public void excel(String excelname,String title,@RequestParam("district3") String district3,@RequestParam("district4") String district4,@RequestParam("pname")String pname,@RequestParam("ename") String ename, OutputStream os,HttpServletResponse response) throws Exception{
response.setContentType("application/vnd.ms-excel");
//response.addHeader("Content-Disposition", "attachment;filename=\""+excelname+".xls\"");
String excelname_=new String(excelname.getBytes("gbk"), "ISO8859-1");
response.addHeader("Content-Disposition", "attachment; filename=\""
+ excelname_ + ".xls\"");
WritableWorkbook workbook = Workbook.createWorkbook(os);
WritableSheet sheet = workbook.createSheet(excelname, 0); //生成名为excelname的工作表,參数0表示这是第一页 
   WritableFont font1= new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD); 
WritableCellFormat format1=new WritableCellFormat(font1); 
format1.setAlignment(jxl.format.Alignment.CENTRE);//设置为居中
format1.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直居中;
format1.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN,Colour.BLACK);
format1.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.NONE);
//给列设置宽度;  
sheet.setColumnView(0,10);
sheet.setColumnView(1,35);
        sheet.setColumnView(2, 35);  
        sheet.setColumnView(3, 15);  
        sheet.setColumnView(4, 15);  
        sheet.setColumnView(5, 15);
        sheet.setColumnView(6, 35);
        sheet.setColumnView(7, 35);
        sheet.setColumnView(8, 20);
        sheet.setColumnView(9, 35);

sheet.mergeCells(0,0,9,0);
Label label=new Label(0,0,title,format1); //创建表头, 在Label对象的构造子中指名单元格位置是第一列第一行(0,0) 
   sheet.addCell(label); 
   sheet.setRowView(0,800);//设置第一行高度
   
   //设置填报单位
   sheet.mergeCells(0,1,9,1);
   font1= new WritableFont(WritableFont.TIMES,14,WritableFont.BOLD); 
   format1=new WritableCellFormat(font1); 
   format1.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN,Colour.BLACK);
   format1.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直居中;
label=new Label(0,1,"填报单位: (盖章)",format1); //创建表头, 在Label对象的构造子中指名单元格位置是第一列第一行(0,0) 
   sheet.addCell(label); 
   sheet.setRowView(1,600);//设置第一行高度
   
   sheet.setRowView(2,500);
font1=new WritableFont(WritableFont.createFont("楷体_GB2312"),12,WritableFont.BOLD);
WritableCellFormat format2=new WritableCellFormat(font1);
format2.setAlignment(jxl.format.Alignment.CENTRE);//设置为居中
format2.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直居中;
//format2.set
//设置边框
   format2.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,Colour.BLACK);
     
   int startindex=2;
label=new Label(0,startindex,"序号",format2); 
   sheet.addCell(label); 
label=new Label(1,startindex,"演练组织单位",format2);
sheet.addCell(label); 
label=new Label(2,startindex,"演练科目名称",format2);
sheet.addCell(label); 
label=new Label(3,startindex,"演练类型",format2);
sheet.addCell(label); 
label=new Label(4,startindex,"演练内容",format2);
sheet.addCell(label); 
label=new Label(5,startindex,"演练时间",format2);
sheet.addCell(label);
label=new Label(6,startindex,"演练地点",format2);
sheet.addCell(label);
label=new Label(7,startindex,"拟參加应急演练的单位及部门",format2);
sheet.addCell(label);
label=new Label(8,startindex,"拟參加演练的人数",format2);
sheet.addCell(label);
label=new Label(9,startindex,"备注",format2);
sheet.addCell(label);

/*生成一个保存数字的单元格 
必须使用Number的完整包路径,否则有语法歧义 
单元格位置是第二列。第一行,值为789.123 
jxl.write.Number number = new jxl.write.Number(2,0,789.123); 
sheet.addCell(number); */
jxl.write.Number number=null;


//从数据库查询数据
Map<String, String> map=new HashMap<String, String>();
Map<String, String> like=new HashMap<String, String>();
if(StringUtils.notNull(district3)){
map.put("s.district3", district3);
}
if(StringUtils.notNull(district3)){
map.put("s.district4", district4);
}
if(StringUtils.notNull(pname)){
map.put("plansdrillname", pname);
}
if(StringUtils.notNull(ename)){
like.put("s.enterprisename", ename);
}
List<CpPlansdrillDto> dtos = cpPlansdrillService.getStatisticsList(map, like, 1,10000);
font1=new WritableFont(WritableFont.createFont("楷体_GB2312"),10,WritableFont.NO_BOLD);
format2=new WritableCellFormat(font1);
format2.setAlignment(jxl.format.Alignment.CENTRE);//设置为居中
format2.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直居中;
format2.setWrap(true);//自己主动换行
format2.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,Colour.BLACK);
//数据保存
for (int i = 0; i <dtos.size(); i++) 
{    
CpPlansdrillDto m=dtos.get(i);
String data="";
if(m.getPlansdrilldate()!=null){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
java.util.Date date=new java.util.Date();  
data=sdf.format(date);
}
int pcount=0;
if(StringUtils.notNull(m.getPlansdrillcount())){
pcount=Integer.parseInt(m.getPlansdrillcount());
}
String deparmetn=StringUtils.notNull(m.getPlansdrilldepartment())?

m.getPlansdrilldepartment():"";
number = new jxl.write.Number(0,i+startindex+1,i+1,format2); 
   sheet.addCell(number);
label=new Label(1,i+startindex+1,m.getPlansdrillunits(),format2);
sheet.addCell(label);
label=new Label(2,i+startindex+1,m.getPlansdrillname(),format2);
sheet.addCell(label);
label=new Label(3,i+startindex+1,m.getPlansdrilltype().equals("1")?

"现场演练":"桌面演练",format2);
sheet.addCell(label);
label=new Label(4,i+startindex+1,m.getPlansdrillcontent().equals("1")?

"单项演练":"综合演练",format2);
sheet.addCell(label);
label=new Label(5,i+startindex+1,data,format2);  
   sheet.addCell(label); 
label=new Label(6,i+startindex+1,m.getPlansdrilladdress(),format2);
sheet.addCell(label);
label=new Label(7,i+startindex+1,deparmetn,format2);
sheet.addCell(label);
number=new jxl.write.Number(8,i+startindex+1,pcount,format2);
   sheet.addCell(number);
label=new Label(9,i+startindex+1,m.getNote(),format2);
sheet.addCell(label);
    }

workbook.write();
workbook.close();
os.close();
response.flushBuffer();
}










本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5117018.html,如需转载请自行联系原作者

相关文章
|
3月前
|
Python
Excel中如何批量重命名工作表与将每个工作表导出到单独Excel文件
本文介绍了如何在Excel中使用VBA批量重命名工作表、根据单元格内容修改颜色,以及将工作表导出为独立文件的方法。同时提供了Python实现导出工作表的代码示例,适用于自动化处理Excel文档。
|
11月前
|
Java API Apache
Java编程如何读取Word文档里的Excel表格,并在保存文本内容时保留表格的样式?
【10月更文挑战第29天】Java编程如何读取Word文档里的Excel表格,并在保存文本内容时保留表格的样式?
703 5
|
10月前
|
人工智能 自然语言处理 Java
FastExcel:开源的 JAVA 解析 Excel 工具,集成 AI 通过自然语言处理 Excel 文件,完全兼容 EasyExcel
FastExcel 是一款基于 Java 的高性能 Excel 处理工具,专注于优化大规模数据处理,提供简洁易用的 API 和流式操作能力,支持从 EasyExcel 无缝迁移。
1978 65
FastExcel:开源的 JAVA 解析 Excel 工具,集成 AI 通过自然语言处理 Excel 文件,完全兼容 EasyExcel
|
8月前
|
文字识别 BI
【图片型PDF】批量识别扫描件PDF指定区域局部位置内容,将识别内容导出Excel表格或批量改名文件,基于阿里云OCR对图片型PDF识别改名案例实现
在医疗和政务等领域,图片型PDF文件(如病历、报告、公文扫描件)的处理需求广泛。通过OCR技术识别这些文件中的文字信息,提取关键内容并保存为表格,极大提高了信息管理和利用效率。本文介绍一款工具——咕嘎批量OCR系统,帮助用户快速处理图片型PDF文件,支持区域识别、内容提取、导出表格及批量改名等功能。下载工具后,按步骤选择处理模式、进行区域采样、批量处理文件,几分钟内即可高效完成数百个文件的处理。
814 8
|
11月前
|
Java BI API
Java Excel报表生成:JXLS库的高效应用
在Java应用开发中,经常需要将数据导出到Excel文件中,以便于数据的分析和共享。JXLS库是一个强大的工具,它基于Apache POI,提供了一种简单而高效的方式来生成Excel报表。本文将详细介绍JXLS库的使用方法和技巧,帮助你快速掌握Java中的Excel导出功能。
324 6
|
11月前
|
数据格式 UED
记录一次NPOI库导出Excel遇到的小问题解决方案
【11月更文挑战第16天】本文记录了使用 NPOI 库导出 Excel 过程中遇到的三个主要问题及其解决方案:单元格数据格式错误、日期格式不正确以及合并单元格边框缺失。通过自定义单元格样式、设置数据格式和手动添加边框,有效解决了这些问题,提升了导出文件的质量和用户体验。
800 3
|
11月前
|
前端开发
实现Excel文件和其他文件导出为压缩包,并导入
实现Excel文件和其他文件导出为压缩包,并导入
201 1
|
11月前
|
Java API Apache
|
11月前
|
存储 Java API
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
766 4
|
3月前
|
Python
如何根据Excel某列数据为依据分成一个新的工作表
在处理Excel数据时,我们常需要根据列值将数据分到不同的工作表或文件中。本文通过Python和VBA两种方法实现该操作:使用Python的`pandas`库按年级拆分为多个文件,再通过VBA宏按班级生成新的工作表,帮助高效整理复杂数据。