一个关于导出excel模板的实例

1 首先jsp页面

点击模板下载,会自动下载模板excel,效果如下

让我们看源码:

1 jsp页面

<div class="tab-pane" id="profile">
&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" id="export" onClick="toModelExport()" value="模板下载" >
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" id="export" onClick="toExport()" value="批量导入" >
</div>

主要是两个按钮

js函数

function toModelExport(){

$("<form>").attr({
"action":"${ctx}/doctor/toModelDownLoad",
"method":"get"
}).submit();toModelExport

}

2后台controller

//模板导入
@RequestMapping(value = "/toModelDownLoad")
public void toModelDownLoad(HttpServletRequest request, HttpServletResponse response) {
WritableWorkbook wb = null;
OutputStream os = null;
String fileName = "doctorImportStand.xls";
try {
os = response.getOutputStream();
wb =new Importmodel().createWorkbookModel(os);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
wb.write();
wb.close();
os.close();
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}

核心就是这个new Importmodel().createWorkbookModel(os);

3Importmodel类

public class Importmodel {
protected final Log log = LogFactory.getLog(getClass());
//医生模板
public WritableWorkbook createWorkbookModel(OutputStream outputStream) {
WritableWorkbook wwb = null;
try {
wwb = Workbook.createWorkbook(outputStream);
WritableSheet sheet =wwb.createSheet("医生信息录入模板",1);
//解决jxl下拉框254字符限制
WritableSheet sheet2 = wwb.createSheet("录入说明", 1);
WritableFont font = new WritableFont(jxl.write.WritableFont
.createFont("隶书"),14);
WritableCellFormat format = new WritableCellFormat(font);
Label label1 = new Label(0,0,"性别",format);
sheet2.addCell(label1);
label1 = new Label(0,1,"1",format);
sheet2.addCell(label1);
label1 = new Label(1,1,"男",format);
sheet2.addCell(label1);
label1 = new Label(0,2,"2",format);
sheet2.addCell(label1);
label1 = new Label(1,2,"女",format);
sheet2.addCell(label1);
label1 = new Label(0,3,"用户类型",format);
sheet2.addCell(label1);
label1 = new Label(0,4,"1",format);
sheet2.addCell(label1);
label1 = new Label(1,4,"医生",format);
sheet2.addCell(label1);
label1 = new Label(0,5,"2",format);
sheet2.addCell(label1);
label1 = new Label(1,5,"患者",format);
sheet2.addCell(label1);
label1 = new Label(0,6,"3",format);
sheet2.addCell(label1);
label1 = new Label(1,6,"专家",format);
sheet2.addCell(label1);
label1 = new Label(0,7,"职称",format);
sheet2.addCell(label1);
label1 = new Label(0,8,"1",format);
sheet2.addCell(label1);
label1 = new Label(1,8,"主任医师",format);
sheet2.addCell(label1);
label1 = new Label(0,9,"2",format);
sheet2.addCell(label1);
label1 = new Label(1,9,"副主任医师",format);
sheet2.addCell(label1);
label1 = new Label(0,10,"3",format);
sheet2.addCell(label1);
label1 = new Label(1,10,"主治医师",format);
sheet2.addCell(label1);
label1 = new Label(0,11,"4",format);
sheet2.addCell(label1);
label1 = new Label(1,11,"医师",format);
sheet2.addCell(label1);
label1 = new Label(0,12,"5",format);
sheet2.addCell(label1);
label1 = new Label(1,12,"教授",format);
sheet2.addCell(label1);
label1 = new Label(0,13,"6",format);
sheet2.addCell(label1);
label1 = new Label(1,13,"副教授",format);
sheet2.addCell(label1);
label1 = new Label(0,14,"医院代码",format);
sheet2.addCell(label1);
label1 = new Label(0,15,"1",format);
sheet2.addCell(label1);
label1 = new Label(1,15,"中山医院",format);
sheet2.addCell(label1);

label1 = new Label(0,16,"2",format);
sheet2.addCell(label1);
label1 = new Label(1,16,"金泽镇社区卫生服务中心",format);
sheet2.addCell(label1);
label1 = new Label(0,17,"3",format);
sheet2.addCell(label1);
label1 = new Label(1,17,"青浦区中心医院",format);
sheet2.addCell(label1);
label1 = new Label(0,18,"组织架构",format);
sheet2.addCell(label1);
label1 = new Label(0,19,"1",format);
sheet2.addCell(label1);
label1 = new Label(1,19,"肺癌联盟",format);
sheet2.addCell(label1);
label1 = new Label(0,20,"科室",format);
sheet2.addCell(label1);
label1 = new Label(0,21,"1",format);
sheet2.addCell(label1);
label1 = new Label(1,21,"呼吸内科",format);
sheet2.addCell(label1);
label1 = new Label(0,22,"2",format);
sheet2.addCell(label1);
label1 = new Label(1,22,"全科",format);
sheet2.addCell(label1);
sheet.mergeCells(0,0,13,0);
sheet.setRowView(0, 400);
StringBuffer strBuf = new StringBuffer();
strBuf.append("属性说明: \r\n 1、用户类型:1 医生 2患者 3专家 此处只能导入医生")
.append("\r\n").append(" 2、性别:1 男 2 女")
.append("\r\n").append(" 3、组织架构为用户所在的组织架构Id 1 肺癌联盟")
.append("\r\n").append(" 4、医院,科室均输入编号即可")
.append("\r\n").append(" 5、 职称:1 主任医师 2 副主任医师 3 主治医师4 医师 5 教授 6 副教授")
.append("\r\n").append(" 6、 请严格按照模板格式进行导入,每一项都要填写,否则不能导入");
Label label = new Label(0,0,strBuf.toString());
sheet.addCell(label);
label = new Label(0,1,"用户类型");
sheet.addCell(label);
label = new Label(0,2,"1");
sheet.addCell(label);
label = new Label(1,1,"手机号");
sheet.addCell(label);
label = new Label(1,2,"13524929813");
sheet.addCell(label);
label = new Label(2,1,"姓名");
sheet.addCell(label);
label = new Label(2,2,"赵");
sheet.addCell(label);
label = new Label(3,1,"性别");
sheet.addCell(label);
label = new Label(3,2,"2");
sheet.addCell(label);
label = new Label(4,1,"组织架构");
sheet.addCell(label);
label = new Label(4,2,"1");
sheet.addCell(label);
label = new Label(5,1,"医院");
sheet.addCell(label);
label = new Label(5,2,"2");
sheet.addCell(label);
label = new Label(6,1,"科室");
sheet.addCell(label);
label = new Label(6,2,"2");
sheet.addCell(label);
label = new Label(7,1,"职称");
sheet.addCell(label);
label = new Label(7,2,"1");
sheet.addCell(label);
label = new Label(8,1,"出生日期");
sheet.addCell(label);
label = new Label(8,2,"1980.08.08");
sheet.addCell(label);
label = new Label(9,1,"医生角色");
sheet.addCell(label);
label = new Label(9,2,"3");
sheet.addCell(label);
label = new Label(10,1,"坐诊时间");
sheet.addCell(label);
label = new Label(10,2,"1-0,1-1,2-0,2-1");
sheet.addCell(label);
label = new Label(11,1,"会诊时间");
sheet.addCell(label);
label = new Label(11,2,"3");
sheet.addCell(label);
label = new Label(12,1,"会诊价格");
sheet.addCell(label);
label = new Label(12,2,"2000");
sheet.addCell(label);
label = new Label(13,1,"简介");
sheet.addCell(label);
label = new Label(13,2,"副教授、医学博士、呼吸内科执行主任,青浦区学科带头人、上海市医学会肺科学会青年委员。从事呼吸内科专业临床医疗、科研及教学工作20年,基础理论知识扎实,擅长慢性阻塞性肺疾病、支气管哮喘、肺心病、呼吸衰竭、肺部感染性疾病、肺部肿瘤、间质性肺疾病、胸膜疾病等呼吸系统常见疾病的诊治,对于呼吸系统各种疑难、危重病症的诊治及抢救技术,机械通气技术和呼吸内镜及介入诊疗技术具有丰富的临床经验。 近年来,主持市级科研课题2项,参与国家自然基金项目、市级科研课题项目多项;作为第一作者在SCI收录及国内核心期刊发表专业学术论文20余篇。");
sheet.addCell(label);
} catch (IOException e) {
log.error("ERROR:createWorkbookModel",e);
} catch (RowsExceededException e) {
log.error("ERROR:createWorkbookModel",e);
} catch (WriteException e) {
log.error("ERROR:createWorkbookModel",e);
}
return wwb;
}

至此,可以顺利实现功能

所用到的依赖

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

时间: 2024-10-21 18:26:16

一个关于导出excel模板的实例的相关文章

java 通过Apache poi导出excel代码demo实例

package com.zuidaima.excel.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.

使用JSExcelXML.js导出Excel模板

     老规矩先来个预览图 业务系统显示效果图 导出模板图 功能描述    世间万物总是相生相克,既然我们的客户要求有导出Excel,当然也会提导人Excel,那么问题就来了,我们的业务系统怎么知道用户提供的数据会是怎样的呢,那么用户需要的数据模板就应运而生了,在JSExcelXmL的最新版本中提供模板生成功能. var tmpdlg = $.ExportExcelTmp({options }); tmpdlg.ExportExcelTmp('open'); 2.参数说明 { SheetNam

POI导出EXCEL模板数据

1.apache poi 3.14+版本 package com.jd.medicine.erp.shop.service.util; import java.io.IOException; import java.io.InputStream; import java.util.Date; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apa

jxls导出EXCEL模板

http://jxls.sourceforge.net/ InputStream templateInput = null; InputStream in = null; OutputStream out = null; try { templateInput = request.getSession().getServletContext().getResourceAsStream("/downloads/project/fpfa.xls"); XLSTransformer tran

EasyPOI导入导出Excel

EasyPOI工具可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 导入maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <grou

easypoi导出Excel

1. 添加maven依赖. <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <arti

ASP.NET Core 导入导出Excel xlsx 文件

ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Linux和Mac. EPPlus.Core 是基于EPPlus 更改而来,在Linux 下需要安装libgdiplus . EPPlus:http://epplus.codeplex.com/ EPPlus.Core:https://github.com/VahidN/EPPlus.Core 下面在A

MVC NPOI Linq导出Excel通用类

之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = 0, Float = 1, Double = 2, String = 3, DateTime = 4, Date = 5 } public class ExportFieldInfo { /// <summary> /// 字段名,用于反射获取值 /// </summary> publi

POI下JAVA导出基于模板的Excel

最近做了一个比较麻烦的功能,导出一个复杂格式的EXCEL,因为使用POI已经很长时间了,导出很方便,导入的话格式并不是很支持,所以想到了使用基于模板,JAVA导出的Excel 如何读取Excel模板然后把动态数据写入到模板以生成特定格式的Excel,同样的思路,先来考虑下会涉及到那些对象,和上篇文章读取相比这里涉及到一个写入的步骤,所以JXL必然会提供一个对象来支持写入,这就是WritableWorkbook.那么如何得到到这个类的实例,查看APIDoc发现Workbook类提供了几个 静态的c