java方式实现保存Excel格式数据

更新时间:2023-06-08 20:28:21 阅读: 评论:0

java⽅式实现保存Excel格式数据
在浏览器中下载查询到的数据库数据,全部保存到SXSSFWorkbook(⼯作簿)中的`Sheet sheet =
⽅式⼀:通过JDBCUtils的⽅式导出
(优点:
1、可以不⽤⼀个个去设置excel表格的头⾏字段
2、可以适⽤于查询不通的表,不⽤处理各个表字段不同的问题
1. 前端发送请求到rvlet,把需要的查询条件(参数)传递到后台(此步骤省略...)
2. 接收参数,通过jdbc的⽅法(jdbc连接数据库步骤省略...)查询数据库数据,`getMetaData()`直接操作数据
库便捷,但效率可能较低
3. 通过respon对象获得输出流,将SXSSFWorkbook写⼊到浏览器
//jdbc⽅式查询数据保存到excel
public EiInfo downData(EiInfo inInfo) {
String sql = convertToSQL(date_PRC1, date_PRC2, type);
String sheetName = convertToSheetName(type);
HttpServletRespon respon =(HttpServletRespon) ("respon");
SXSSFWorkbook xswb;
verify
try {
// 创建xlsx
xswb = createXlsx(sql, sheetName);
// ⽣成⽂件
respon.tContentType("application/vnd.ms-excel");
respon.tHeader("Content-Disposition", "attachment;filename=" + de(sheetName, "utf-8"));
hormen
OutputStream outputStream;
outputStream = OutputStream();
xswb.write(outputStream);
outputStream.flush();
outputStream.clo();
} catch (Exception e) {
e.printStackTrace();
inInfo.t("status", fal);
inInfo.t("msg", "导出失败!");
return inInfo;
}
return inInfo;
}
/*
* 传⼊sql语句,和需要的命名
* 返回⼯作簿对象
*/
private SXSSFWorkbook createXlsx(String sql, String sheetName) throws Exception {
SXSSFWorkbook xswb = null;
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = Connection();
st = ateStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
xswb = new SXSSFWorkbook();
// 创建⼯作表
Sheet sheet = ateSheet();
xswb.tSheetName(0, sheetName);
// 查询数据
深圳mbars = st.executeQuery(sql);
System.out.println(rs);
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = ColumnCount();
// 创建表头
Row head = ateRow(0);
for (int i = 0; i < columnCount; i++) {
Cell cell = ateCell(i);
cell.ColumnName(i + 1));
}
// 获取总⾏数
rs.last();
int rowCount = rs.getRow();
rs.beforeFirst();
// 创建标题
for (int i = 1; i <= rowCount; i++) {
<();
water clotRow row = ateRow(i);
for (int j = 0; j < columnCount; j++) {
Cell cell = ateCell(j);
cell.String(j + 1));
}
}
} catch (Exception e) {
throw new Exception(e);
} finally {
}
return xswb;
}
⽅式⼆:通过MyBatis的xml放式导出数据(我觉得适⽤于条件只查询⼀张表数据)          @Override
        protected void doGet(HttpServletRequest req, HttpServletRespon resp) throws ServletException, IOException {
          if (null == req || null == resp){
            return;
          }
          ......
         省略获得JSONArray格式的数据
          //⼆、将数据转成excel
req.tCharacterEncoding("UTF-8");
resp.tCharacterEncoding("UTF-8");
contrastresp.tContentType("application/x-download");
String fileName = convertToSheetName(type);
fileName = de(fileName, "UTF-8");
resp.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 第⼀步:定义⼀个新的⼯作簿
XSSFWorkbook wb = new XSSFWorkbook();
// 第⼆步:创建⼀个Sheet页
XSSFSheet sheet = wb.createSheet("startTimeendTime");
sheet.tDefaultRowHeight((short) (2 * 256));//设置⾏⾼
sheet.tColumnWidth(0, 6000);//设置列宽
sheet.tColumnWidth(3,5500);
sheet.tColumnWidth(4,5500);
sheet.tColumnWidth(8,5500);
sheet.tColumnWidth(9,5500);
sheet.tColumnWidth(10,5500);
sheet.tColumnWidth(11,5500);
XSSFFont font = wb.createFont();
font.tFontName("宋体");
font.tFontHeightInPoints((short) 16);
XSSFRow row = ateRow(0);
XSSFCell cell = ateCell(0);
cell.tCellValue("记录ID");
cell = ateCell(1);权重系数
cell.tCellValue("账套");
cell = ateCell(2);
cell.tCellValue("⽇期");
cell = ateCell(3);
cell.tCellValue("产品属性名称");
cell = ateCell(4);
cell.tCellValue("库存类型名称");
cell = ateCell(5);
cell.tCellValue("⼤类名称");
cell = ateCell(6);
cell.tCellValue("中类名称");
cell = ateCell(7);
cell.tCellValue("物料名称");
//  cell = ateCell(8);
//  cell.tCellValue("⼚别名称");
/
/  cell = ateCell(9);
palmos
//  cell.tCellValue("库存分类名称");
cell = ateCell(8);
cell.tCellValue("部门名称");
//  cell = ateCell(11);
//  cell.tCellValue("库龄名称");
//  cell = ateCell(12);
//  cell.tCellValue("内外销名称");
gemstone//  cell = ateCell(13);
//  cell.tCellValue("库区名称");
//  cell = ateCell(14);
/
关于 英语
/  cell.tCellValue("期货现货名称");
//  cell = ateCell(15);
//  cell.tCellValue("付款⽅式名称");
//  cell = ateCell(16);
//  cell.tCellValue("产品流向名称");
//  cell = ateCell(17);
//  cell.tCellValue("库存标志名称");
cell = ateCell(9);
cell.tCellValue("库存重量");
cell = ateCell(10);
cell.tCellValue("库存⾦额");
cell = ateCell(11);
cell.tCellValue("市场⾦额");
XSSFRow rows;
XSSFCell cells;
for(int i = 0; i < listArray.size(); i++) {
// 第三步:在这个sheet页⾥创建⼀⾏
rows = ateRow(i+1);
// 第四步:在该⾏创建⼀个单元格
cells = ateCell(0);
// 第五步:在该单元格⾥设置值
cells.JSONObject(i).get("REC_ID").toString());
cells = ateCell(1);
cells.JSONObject(i).get("ACCOUNT").toString());
cells = ateCell(2);
cells.JSONObject(i).get("DATE_PRC").toString());
cells = ateCell(3);
cells.JSONObject(i).get("PROD_ATTR_NAME").toString());
cells = ateCell(4);
cells.JSONObject(i).get("INV_TYPE_NAME").toString());
cells = ateCell(5);
cells.JSONObject(i).get("BIGCLASS_NAME").toString());
cells = ateCell(6);
cells.JSONObject(i).get("MIDCLASS_NAME").toString());
cells = ateCell(7);
cells.JSONObject(i).get("MAT_NAME").toString());
//  cells.JSONObject(i).get("FACTORY_NAME").toString());
//  cells.JSONObject(i).get("INV_CAT_NAME").toString());
cells = ateCell(8);
cells.JSONObject(i).get("DEPT_NAME").toString());
//  cells.JSONObject(i).get("INV_AGE_NAME").toString());
//  cells.JSONObject(i).get("DOMESTIC_EXP_NAME").toString()); //  cells.JSONObject(i).get("STORE_NAME").toString());
//  cells.JSONObject(i).get("FUTURES_SPOT_NAME").toString()); //  cells.JSONObject(i).get("PAY_METHOD_NAME").toString()); //  cells.tCel
JSONObject(i).get("PROD_FLOW_NAME").toString());
//  cells.JSONObject(i).get("INV_FLAG_NAME").toString());
cells = ateCell(9);
cells.JSONObject(i).get("INV_WT").toString());
打招呼的英文cells = ateCell(10);
cells.JSONObject(i).get("INV_AMT").toString());
cells = ateCell(11);
cells.JSONObject(i).get("MARKET_AMT").toString());
}
try {
OutputStream out = OutputStream();
wb.write(out);
out.flush();
out.clo();
} catch (IOException e) {
// TODO Auto-generated catch block    e.printStackTrace();
}
}

本文发布于:2023-06-08 20:28:21,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/138476.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:名称   数据   查询
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图