- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章jxl 导出数据到excel的实例讲解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
优点:
Jxl对中文支持非常好,操作简单,方法看名知意.
Jxl是纯javaAPI,在跨平台上表现的非常完美,代码可以再windows或者Linux上运行而无需重新编写 。
支持Excel 95-2000的所有版本(网上说目前可以支持Excel2007了,还没有尝试过) 。
生成Excel 2000标准格式 。
支持字体、数字、日期操作 。
能够修饰单元格属性 。
支持图像和图表,但是这套API对图形和图表的支持很有限,而且仅仅识别PNG格式.
缺点:
效率低,图片支持不完善,对格式的支持不如POI强大 。
案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
String times = (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date());
String fname = "系统日志" + times; // 文件名
List<
Logs
> list=logsService.selectForList(hql.toString());
String path = request.getSession().getServletContext().getRealPath("/")
+ "xls/"
+ (new SimpleDateFormat("yyyyMMdd")).format(new Date());
File file = new File(path);
// 如果文件夹不存在则创建
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
response.setContentType("application/vnd.ms-excel;charset=utf-8");// // 指定文件的保存类型。
response.setCharacterEncoding("utf-8");
ExportUtil.writer_log(request,fname, list, response);//下载到本地
|
writer_log导出方法如下 。
- /**
- * 生成 excel 文件,导出到本地电脑
- * @param fname 文件名
- * @param list 需要打印的数据,即数据库查询的数据列表
- */
- public static void writer_log(HttpServletRequest request,String fname, List list, HttpServletResponse response) {
- try {
- OutputStream os = response.getOutputStream();//取得输出流
- response.reset();//清空输出流
- // 下面是对中文文件名的处理 开始
- response.setCharacterEncoding("UTF-8");//设置相应内容的编码格式
- if(isMsBrowser(request))
- fname= java.net.URLEncoder.encode(fname ,"UTF-8");
- else fname = new String(fname.getBytes("UTF-8"),"ISO-8859-1");
- response.setHeader("Content-Disposition","attachment;filename="+fname+".xls");
- response.setContentType("application/msexcel;charset=utf-8");//定义输出类型
- // 对中文文件名的处理 结束
- // 此处的 Workbook 导入的是 import jxl.Workbook;
- WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
- WritableSheet sheet = wbook.createSheet("系统日志", 0); // 工作表名称
- CellView cellView = new CellView();
- cellView.setAutosize(true); //设置自动大小
- sheet.setColumnView(0, 8); //设置单元格宽度,0是列号,8是宽度
- sheet.setColumnView(1, 20); //设置单元格宽度,1是列号,20是宽度
- sheet.setColumnView(2, 24);
- sheet.setColumnView(3, 20);
- sheet.setColumnView(4, 30);
- sheet.setColumnView(5, 13);
- sheet.setColumnView(6, 15);
- sheet.setColumnView(7, 32);
- sheet.setColumnView(8, 15);
- // 设置Excel字体
- WritableFont wfont = new WritableFont(WritableFont.createFont("宋体"), 22,
- WritableFont.BOLD, false,
- jxl.format.UnderlineStyle.NO_UNDERLINE,
- jxl.format.Colour.BLACK); //设置单元格字体样式
- WritableCellFormat titleFormat = new WritableCellFormat(wfont); //添加单元格字体
- titleFormat.setAlignment(Alignment.CENTRE); //设置文字居中对齐方式;
- String[] title = { "系统日志" };
- // 设置Excel表头 开始
- for (int i = 0; i < title.length; i++) {
- // 此处导入的是 import jxl.write.Label;
- Label excelTitle = new Label(i, 0, title[i], titleFormat); //单元格内容
- // 参数顺序:开始列,开始行,结束列,结束行
- sheet.mergeCells(0, 0, 8, 0); //所在位置,第几行第几列
- sheet.addCell(excelTitle); //添加单元格信息
- }
- // 设置Excel表头 结束
- // 第一行,即显示时间,参数:(所在列,所在行,内容)
- WritableFont wfonttime = new WritableFont(WritableFont.createFont("宋体"), 11,
- WritableFont.NO_BOLD, false,
- jxl.format.UnderlineStyle.NO_UNDERLINE,
- jxl.format.Colour.BLACK); //设置单元格字体样式
- WritableCellFormat titletime = new WritableCellFormat(wfonttime);//添加单元格字体
- titletime.setAlignment(Alignment.RIGHT); //设置文字居中对齐方式;
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- // sheet.setColumnView(1, cellView); //根据内容自动设置列宽
- Label contentDate = new Label(0, 1, df.format(new Date()), titletime); //单元格内容
- // sheet.mergeCells(16, 1, 18, 1); //所在位置,第几行第几列,即合并的位置,如没合并,可不写
- sheet.addCell(contentDate); //添加单元格信息
- // 第一行 结束
- // 第二行,显示条件标题栏
- WritableFont wfont2 = new WritableFont(WritableFont.createFont("宋体"), 11,
- WritableFont.BOLD, false,
- jxl.format.UnderlineStyle.NO_UNDERLINE,
- jxl.format.Colour.BLACK); //设置单元格字体样式
- WritableCellFormat titleFormat2 = new WritableCellFormat(wfont2);//添加单元格字体
- titleFormat2.setBorder(Border.ALL, BorderLineStyle.THIN); //设置边框--实线;
- titleFormat2.setAlignment(Alignment.CENTRE); //设置文字居中对齐方式;
- titleFormat2.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直居中;
- Label content2a = new Label(0, 2, "序号", titleFormat2); //单元格内容--第1格
- Label content2b = new Label(1, 2, "用户名", titleFormat2); //单元格内容--第2格
- Label content2c = new Label(2, 2, "记录时间", titleFormat2); //单元格内容--第3格
- Label content2d = new Label(3, 2, "操作模块", titleFormat2); //单元格内容--第4格
- Label content2e = new Label(4, 2, "操作内容", titleFormat2); //单元格内容--第5格
- Label content2f = new Label(5, 2, "操作动作", titleFormat2); //单元格内容--第6格
- Label content2g = new Label(6, 2, "操作人IP", titleFormat2); //单元格内容--第7格
- Label content2h = new Label(7, 2, "所属组织", titleFormat2); //单元格内容--第8格
- Label content2i = new Label(8, 2, "备注", titleFormat2); //单元格内容--第9格
- sheet.mergeCells(0, 1, 8, 1);
- /*sheet.mergeCells(0, 2, 0, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第1列)
- sheet.mergeCells(1, 2, 1, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第2列)
- sheet.mergeCells(2, 2, 2, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第3列)
- sheet.mergeCells(3, 2, 3, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第4列)
- sheet.mergeCells(4, 2, 4, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第5列)
- sheet.mergeCells(5, 2, 5, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第6列)
- sheet.mergeCells(6, 2, 6, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第7列)
- sheet.mergeCells(7, 2, 7, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第8列)
- sheet.mergeCells(8, 2, 8, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第9列)
- sheet.mergeCells(9, 2, 9, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第10列)
- sheet.mergeCells(10, 2, 12, 3); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到4行,第11-13列)
- sheet.mergeCells(13, 2, 15, 3); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到4行,第14-16列)
- sheet.mergeCells(16, 2, 16, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第17列)
- sheet.mergeCells(17, 2, 17, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第18列)
- sheet.mergeCells(18, 2, 18, 4); //所在位置,第几行第几列,即合并的位置,如没合并,可不写;(此处为第3行开始到5行,第19列)
- */ sheet.addCell(content2a); //添加第1格单元格信息
- sheet.addCell(content2b); //添加第2格单元格信息
- sheet.addCell(content2c); //添加第3格单元格信息
- sheet.addCell(content2d); //添加第4格单元格信息
- sheet.addCell(content2e); //添加第5格单元格信息
- sheet.addCell(content2f); //添加第6格单元格信息
- sheet.addCell(content2g); //添加第7格单元格信息
- sheet.addCell(content2h); //添加第8格单元格信息
- sheet.addCell(content2i); //添加第9格单元格信息
- WritableFont wf = new WritableFont(WritableFont.createFont("宋体"), 11,
- WritableFont.NO_BOLD, false,
- jxl.format.UnderlineStyle.NO_UNDERLINE,
- jxl.format.Colour.BLACK); //设置单元格字体样式
- WritableCellFormat wcf = new WritableCellFormat(wf); //添加单元格字体
- wcf.setBorder(Border.ALL, BorderLineStyle.THIN); //设置边框--实线;
- wcf.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直对齐
- wcf.setAlignment(Alignment.CENTRE); //设置文字水平居中对齐方式;
- wcf.setWrap(true); //自动换行
- WritableFont wf1 = new WritableFont(WritableFont.createFont("宋体"), 11,
- WritableFont.NO_BOLD, false,
- jxl.format.UnderlineStyle.NO_UNDERLINE,
- jxl.format.Colour.BLACK); //设置单元格字体样式
- WritableCellFormat wcf1 = new WritableCellFormat(wf1); //添加单元格字体
- wcf1.setBorder(Border.LEFT, BorderLineStyle.THIN); //设置边框--实线;
- wcf1.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直对齐
- wcf1.setAlignment(Alignment.CENTRE); //设置文字水平居中对齐方式;
- // 以下循环数据库获取的信息
- int c = 1; // 用于循环时Excel的行号
- Iterator it = list.iterator();
- while (it.hasNext()) {
- Logs tc = (Logs) it.next();
- DateFormat dfmt = new SimpleDateFormat("yyyy.MM");
- String xh = String.valueOf(c);
- if(xh== null){
- xh = "";
- }
- Label content0 = new Label(0, c+2, xh, wcf); //序号
- String name = tc.getUserName();
- if(name == null){
- name = "";
- }
- Label content1 = new Label(1, c+2, name, wcf); //用户名
- String xrPresent = tc.getLogTime();
- if(xrPresent == null){
- xrPresent = "";
- }
- Label content2 = new Label(2, c+2, xrPresent, wcf); //记录时间
- String czModel = tc.getModel();
- if(czModel == null){
- czModel = "";
- }
- Label content3 = new Label(3, c+2, czModel, wcf); //操作模块
- String sex = tc.getContent();
- if(sex == null){
- sex = "";
- }
- Label content4 = new Label(4, c+2, sex, wcf); //操作内容
- String birthday = tc.getOperate();
- if(birthday == null){
- birthday = "";
- }
- Label content5 = new Label(5, c+2, birthday, wcf); //操作动作
- String nation = tc.getIp();
- if(nation == null){
- nation = "";
- }
- Label content6 = new Label(6, c+2, nation, wcf); //操作人IP
- String origin = tc.getOrgName();
- if(origin == null){
- origin = "";
- }
- Label content7 = new Label(7, c+2, origin, wcf); //所属组织
- String bPlace = tc.getRemark();
- if(bPlace == null){
- bPlace = "";
- }
- Label content8 = new Label(8, c+2, bPlace, wcf); //备注
- String abc="";
- Label content9 = new Label(9, c+2, abc, wcf1); //备注
- sheet.setRowView(c+2, 600); // 设置行高
- sheet.setRowView(c+2, 600);
- sheet.setRowView(c+2, 600);
- sheet.setRowView(c+2, 600);
- sheet.setRowView(c+2, 600);
- sheet.setRowView(c+2, 600);
- sheet.setRowView(c+2, 600);
- sheet.setRowView(c+2, 600);
- sheet.setRowView(c+2, 600);
- sheet.setRowView(c+2, 600);
- sheet.mergeCells(0, c+2, 0, c+2); // 合并第一列第c+2行到第一列第c+2行的所有单元格
- sheet.mergeCells(1, c+2, 0, c+2); //mergeCells(a,b,c,d) 单元格合并函数
- sheet.mergeCells(2, c+2, 0, c+2); //a 单元格的列号
- sheet.mergeCells(3, c+2, 0, c+2); //b 单元格的行号
- sheet.mergeCells(4, c+2, 0, c+2); //c 从单元格[a,b]起,向下合并到c列
- sheet.mergeCells(5, c+2, 0, c+2); //d 从单元格[a,b]起,向下合并到d行
- sheet.mergeCells(6, c+2, 0, c+2);
- sheet.mergeCells(7, c+2, 0, c+2);
- sheet.mergeCells(8, c+2, 0, c+2);
- sheet.mergeCells(9, c+2, 0, c+2);
- sheet.addCell(content0);
- sheet.addCell(content1);
- sheet.addCell(content2);
- sheet.addCell(content3);
- sheet.addCell(content4);
- sheet.addCell(content5);
- sheet.addCell(content6);
- sheet.addCell(content7);
- sheet.addCell(content8);
- sheet.addCell(content9);
- c++;
- }
- wbook.write(); // 写入文件
- wbook.close();
- os.close();
- } catch (Exception e) {
- throw new PaikeException("导出文件出错");
- }
- }
原文链接:http://www.cnblogs.com/learnapi/p/8027947.html 。
最后此篇关于jxl 导出数据到excel的实例讲解的文章就讲到这里了,如果你想了解更多关于jxl 导出数据到excel的实例讲解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想制作一个引用另一个 excel 文件中的单元格的公式。我已经弄清楚了,如下所示: ='C:\Users\17\Desktop\[JAN-11 2011.xlsx]1'!$H$44 但由于此工作表中
有谁知道是否可以在 Excel 中生成缺少地址门牌号的报告? 例如,我们在 Apple St (no.5, 9, 11) 有三个地址记录,是否可以生成一个报告: 列出工作簿中每条街道的所有记录街道编号
这个问题已经有答案了: VBA auto hide ribbon in Excel 2013 (7 个回答) 已关闭 4 年前。 我试图在打开工作文件时隐藏我的丝带。 我已点击以下链接,但不断收到运行
我编写了一个 VBA 程序来删除元音。我无法从 excel 调用该函数。我收到 #NAME 错误。下面的代码 Function REMOVEVOWELS(Txt) As String 'Removes
嗨,我正在尝试在 MS excel 中应用一个函数(正确函数) 但是当我编写这个函数并使用填充句柄将其复制到其他单元格时,我在所有复制的单元格中得到相同的输出。 但是当我点击单元格时,引用是好的。但结
假设我有一个格式如下的电子表格: Sheet 1 | Sheet 2 name email | name e
我正在尝试简化财务报告中的数据输入,因此我尝试使用 Excel Visual Basic 制作表格。 到目前为止我做了2个用户表单,以后我会做5个。我做了用户表单,以便数据输入运算符(operator
我需要对单元格公式而不是单元格内容执行 Mid 或 Find。 如果我的单元格公式是: =[功能](Arg1, Arg2, Arg3) 我需要能够将 Arg2 提取到另一个单元格。 如果不使用 VBA
我想用 VBA 管理嵌入在另一个 Excel 文件中的 Excel 文件。我可以使用 .docx 文档找到很多结果,但我坚持使用 .xlsx 文档。 我最后一次尝试是使用 OLE 对象,但停留在“Sa
我最近一直在尝试使用 perl 和一些模块来读取 Excel 文件,尤其是单元格的格式。 例如,我写了一段使用 ParseExcel 模块读取单元格背景颜色的 perl 代码。然而,在测试时我注意到对
我目前正在使用 Maatwebsite 的 Excel 包,并且能够很好地生成一个包含我想要的列和值的表格,但我希望能够生成表格,其他表格位于单个 Excel 工作表的下方。可能吗? 上面附上的屏幕截
我需要以下方面的指导。我有一个包含 150000 条记录的文件 (excel)。收到另一个包含 5000-6000 条记录的 excel 文件,需要根据第二个文件中信息的某些条件删除该行。 我使用字典
我有我认为的标准公式,根据我使用的 Excel 版本、Excel 365 或 Excel 2019 的不同,它的行为会有所不同 =IF(F5=$M$1;IFERROR(IF(AND(IFERROR(F
信息: 我有一个名为 Demo.xlsm 的 Excel 文件 此文件包含一个名为 UserForm1 的用户表单,该用户表单会在打开文件时自动加载。 打开文件时,名为 Demo.xlsm 的工作簿也
我在A Excel工作表中有一个列,其值是1 1 1 2 2 2 3 3 3 4 4 4....,在B Excel工作表中有另一列,其值1 2 4 ....,什么我想要的是从 B 读取值并查看它们是否
所以,我有这个问题,我想通过使用 OR 函数检查调整列的条件来找到列的平均值,我尝试将 OR 放入 AverageIf 函数,失败,还尝试了“Average(IF( OR("再次不是正确的返回。认为这
假设我想要这种类型的formula = SUM(startcell:endcell)的答案,但是startcell和endcell组件发生了变化。 因此,我希望能够使用 和 中的任何值,而不是直接在公
我正在寻找一个简单的 Excel 宏,它可以根据单元格中的特定数字/值将行从一张工作表复制到 Excel 中的另一张工作表。我有两张纸。一个称为“master”,另一个表称为“top10”。 这是数据
我正在尝试调用另一个工作簿中的 Excel 宏。它是一个特定于工作表的宏,但 Microsoft 文档和网上研究给出的语法仅提供了一种仅通过工作簿访问宏的方法。该语法是: Application.Ru
我检查了很多不同的帖子,但似乎找不到我正在寻找的确切代码。另外,我以前从未使用过 VBA,因此我尝试从其他帖子中获取代码并输入我的信息以使其正常工作。还没有运气。在工作中,我们有一个 Excel 薪资
我是一名优秀的程序员,十分优秀!