数据库数据导出Excel
2016-05-24 09:16
302 查看
package com.haiersoft.buztest.util;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelMethod {
public void toExcelTest(List stulist, HttpServletResponse response){
//创建工作流
OutputStream os = null;
//初始化工作表
WritableWorkbook workbook = null;
try {
//设置弹出对话框
response.setContentType("application/DOWLOAD");
//设置工作表的标题
response.setHeader("Content-Disposition", "attachment; filename=My Test TO Excel.xls");
os = response.getOutputStream();
//创建工作表
workbook = Workbook.createWorkbook(os);
//定义工作表 sheet 标题
WritableSheet ws = workbook.createSheet("Mapping", 0);
ws.getSettings().setShowGridLines(true);
ws.getSettings().setProtected(false);
//控制列的宽度,如果你要不给一样的宽度,就单独写,i代表的是列的下标,从0开始 ,从左到右
for(int i=0;i<7;i++){
ws.setColumnView(i, 20);
}
// 創建标题列名称 (此处可以利用一维数组输出,会更简单)
Label titleLabel = null;
titleLabel = new Label(0, 0, "StuNo", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(1, 0, "StuName", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(2, 0, "StuMoniter", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(3, 0, "StuAddr", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(4, 0, "Stu TELL", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(5, 0, "Stu sex", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(6, 0, "Stu classNo", getHeadFormat());
ws.addCell(titleLabel);
workbook.write();
workbook.close();
os.close();
} catch (Exception e) {
System.out.println(e.getCause());
System.out.println(e.getMessage());
}
}
public static WritableCellFormat getHeadFormat() throws Exception {
//设置字体
WritableFont wf = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD);
//创建单元格FORMAT
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setLocked(true);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
wcf.setBackground(Colour.GREY_25_PERCENT);
return wcf;
}
}
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelMethod {
public void toExcelTest(List stulist, HttpServletResponse response){
//创建工作流
OutputStream os = null;
//初始化工作表
WritableWorkbook workbook = null;
try {
//设置弹出对话框
response.setContentType("application/DOWLOAD");
//设置工作表的标题
response.setHeader("Content-Disposition", "attachment; filename=My Test TO Excel.xls");
os = response.getOutputStream();
//创建工作表
workbook = Workbook.createWorkbook(os);
//定义工作表 sheet 标题
WritableSheet ws = workbook.createSheet("Mapping", 0);
ws.getSettings().setShowGridLines(true);
ws.getSettings().setProtected(false);
//控制列的宽度,如果你要不给一样的宽度,就单独写,i代表的是列的下标,从0开始 ,从左到右
for(int i=0;i<7;i++){
ws.setColumnView(i, 20);
}
// 創建标题列名称 (此处可以利用一维数组输出,会更简单)
Label titleLabel = null;
titleLabel = new Label(0, 0, "StuNo", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(1, 0, "StuName", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(2, 0, "StuMoniter", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(3, 0, "StuAddr", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(4, 0, "Stu TELL", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(5, 0, "Stu sex", getHeadFormat());
ws.addCell(titleLabel);
titleLabel = new Label(6, 0, "Stu classNo", getHeadFormat());
ws.addCell(titleLabel);
workbook.write();
workbook.close();
os.close();
} catch (Exception e) {
System.out.println(e.getCause());
System.out.println(e.getMessage());
}
}
public static WritableCellFormat getHeadFormat() throws Exception {
//设置字体
WritableFont wf = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD);
//创建单元格FORMAT
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setLocked(true);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
wcf.setBackground(Colour.GREY_25_PERCENT);
return wcf;
}
}
相关文章推荐
- oracle数据库 ORA-01461: can bind a LONG value only for insert into a LONG column解决方案
- 一天一点MySQL复习——获取数据库系统时间、变量赋值、变量比较
- 解决SQL SERVER 2008数据库表中修改字段后不能保存
- 几种免费的数据库建模工具
- 几种免费的数据库建模工具
- MySQL 乱码之我见
- MySQL数据库中存储引擎问题
- SQL Server2012无法连接到服务器
- 实用知识:SQL 常用指令(增删改查 )
- C#使用Redis集群缓存
- 第107课: Spark Streaming电商广告点击综合案例底层数据层的建模和编码实现(基于MySQL)
- mysql基本应用(实例)
- JDBC 数据库连接池 小结
- 图标
- EF结合SqlBulkCopy实现高效的批量数据插入 |EF插件EntityFramework.Extended实现批量更新和删除
- MySQL 5.6内存占用过高解决方案【总结】
- MySQL索引原理及慢查询优化
- SQL Server error '80040e14'的处理
- 容易被忽略的事----sql语句中select语句的执行顺序
- Python3连接MySQL(pymysql)模拟转账实现代码