数据库数据导出Excel
来源:互联网 发布:吉林动画学院网络 编辑:程序博客网 时间:2024/05/17 09:16
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;
}
}
0 0
- 数据库数据导出Excel
- 导出数据库数据到Excel
- 导出数据库数据到Excel
- 导出数据库数据到Excel
- 数据库数据导出到excel
- EXCEL大数据数据库导出
- excel模式数据库导出数据
- SSM数据库数据导出excel
- 数据库数据导出成excel,excel数据导入数据库
- EXCEL导入数据库及数据库数据导出到EXCEL
- 从数据库导出数据到Excel
- 把数据库中的数据导出到excel
- 数据库中数据导出成excel
- poi将数据库数据导出excel文件
- 导出数据库数据到excel文件
- 数据库数据批量导出到excel
- Sybase数据库大量数据导出Excel
- PHP导出数据库数据至Excel
- java web前端攻城狮 学习路线图
- Java Iterator接口简单实例
- cmd中执行netsh wlan start hostednetwork 无法启动承载网络。 组或资源的状态不是执行请求操作的正确状态。
- java入门教程-1.7Java类和对象的概念
- vsftpd配置文件详解
- 数据库数据导出Excel
- 用OpenCV对视频进行截图
- 实习入职第五天:位运算与移运算
- Android 培训课程V1.0
- LeetCode 68. Text Justification
- Hive on Spark入门设置
- android如何制作出一个简单的聊天app
- el表达式的一个小问题
- Echarts:折现图表案例