poi实现读取数据库,新建xlsx,写入数据库数据,设置样式,选择路径,文件下载(原创)...

package cn.vit.auto.proto.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hibernate.Session;
import cn.vit.auto.proto.entity.BookDetails;

/**
 *
 * @author Aubergine_kang
 *
 */
public class DownLoadUtil {
 private static String userName = "";
 private static double sumCount = 0;
 //单元格样式
 private static CellStyle cs1;
 private static CellStyle cs2;
 private static CellStyle cs3;
 
    //创建单元格样式
 public static void newCellStyle(Workbook Book_details) {
  cs1 = Book_details.createCellStyle();
  cs1.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
  cs1.setFillPattern(CellStyle.SOLID_FOREGROUND);

  cs2 = Book_details.createCellStyle();
  cs2.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
  cs2.setFillPattern(CellStyle.SOLID_FOREGROUND);

  cs3 = Book_details.createCellStyle();
  cs3.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  cs3.setFillPattern(CellStyle.SOLID_FOREGROUND);

 }

 // 第一个sheet需要的list从数据库读取
 public static List<BookDetails> getDownLoadList(Session session) {
  String sql = " select id,Book,user,count,month from Book_details "
    + " where month(month) = month(SYSDATE()) "
    + " group by user,Book";
  List<BookDetails> list = (List<BookDetails>) session
    .createSQLQuery(sql).addEntity(BookDetails.class).list();
  return list;
 }

 // 写第一个sheet的内容
 public static void writeDownLoadExcel(List<BookDetails> list, Sheet sheet,
   Session session) throws IOException {
  writeExcelHead(sheet);
  for (int i = 0; i < list.size(); i++) {
    BooksDetails gds = (BooksDetails) list.get(i);
          String name = gds.getUser().getUserName();
    int rows = sheet.getPhysicalNumberOfRows();
    Row row = sheet.createRow(rows);
    row.createCell(0).setCellValue(userName);
    row.getCell(0).setCellStyle(cs3);
    row.createCell(1).setCellValue(userName2);
    row.getCell(1).setCellStyle(cs2);
    session.flush();
    session.clear();
    }
   }
  }

 }
 // 写第一个sheet的头部
 private static void writeExcelHead(Sheet sheet) {

  Row header = sheet.createRow(0);
  header.createCell(0).setCellValue("name");
  header.createCell(1).setCellValue("Book");
  header.createCell(2).setCellValue("count");
  header.createCell(3).setCellValue("sum");

  header.getCell(0).setCellStyle(cs1);
  header.getCell(1).setCellStyle(cs1);
  header.getCell(2).setCellStyle(cs1);
  header.getCell(3).setCellStyle(cs1);
 }

 //弹出保存对话框,选择路径进行保存
 private static void tearDown(HttpServletResponse response,
   Workbook Book_details) throws IOException {
  response.reset();
  response.setContentType("application/x-msdownload");
  Date d = new Date();
  SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
  String name = sf.format(d) + "BookApply.xlsx";

  response.setHeader("Content-Disposition", "attachment;   filename="
    + new String(name.getBytes(), "iso8859-1"));
  ServletOutputStream sos = response.getOutputStream();

  Book_details.write(sos);
  sos.flush();
  sos.close();
 }

 //将各个步骤串起来,提交给外部调用
 public static void downLoad(Session session, HttpServletResponse response) {
  Workbook Book_details = new XSSFWorkbook();
  newCellStyle(Book_details);
  String sheetName = "BookDetails";
  Sheet sheet = Book_details.createSheet(sheetName);
  List<BookDetails> list = getDownLoadList(session);
  try {
   writeDownLoadExcel(list, sheet, session);
   tearDown(response, Book_details);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}

用到的相关的jar包在附件里


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值