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包在附件里
分享到:
相关推荐
使用poi解析excel文件,并将数据写入到数据库 项目说明 这个项目实现的功能是读取excel文件中的数据,解析并写入数据库。 读取的excel文件位于项目目录下的 excel\0805.xlsx 使用IntelliJ IDEA开发此项目 使用MYSQL...
1、POI_EXCEL包下分别有两个类,一个是读取excel内容,一个是想excel写入内容 2、cn.itcast包下的所有包,是为了实现从excel写入到数据库中,和从数据库写入到excel中 注意事项: 1、需要导入maven工程 2、使用...
Java POI根据模板生成Excel文件并写入磁盘,资源文件仅为实现的简单测试Demo,并没有进行代码优化,可以直接导入运行,资源文件仅供参考。
由于项目需要对大量Excel数据进行输入输出处理,在使用JXL,POI后发现很容易出现OOM,最后在网上找到阿里的开源项目EasyExcel能很快速的读取写入超大Excel文件。经过大量的调试优化,现通过JAVA生成104万行20列的...
POIFS (较差混淆技术实现文件系统) : 此组件是所有其他POI元件的基本因素。它被用来明确地读取不同的文件。 HSSF (可怕的电子表格格式) : 它被用来读取和写入MS-Excel文件的xls格式。 XSSF (XML格式) : 它是用于MS...
Apache POI是一个流行的Java库,用于读取和写入Microsoft Office格式的文件,特别是Excel。它是Apache软件基金会的一个开源项目,提供了多种API来处理Office文档,包括Word、Excel、PowerPoint等。Apache POI的主要...
XWPF(XML字处理器格式):用于读取和写入MS-Word的扩展文件 .docx 。 HSLF:用于阅读,创建和编辑PowerPoint演示文稿。 HDGF(Horrible DiaGram格式):它包含MS-Visio二进制文件的类和方法。 HPBF(Horrible PuBlisher格式...
java读写Excel文件所需jar包
Apache POI 文件 入门 步骤1:下载 git clone https://github.com/T5750/poi.git cd poi ... XSSF –是POI项目对Excel 2007 OOXML(.xlsx)文件格式的纯Java实现。 例如 , 。 SXSSF (自3.8-beta
在项目中,经常免不了要导入、导出Excel,导出Excel稍微简单点,制作一张模板,然后将查询之后的数据写入到模板中即可,导出程序可以做到一次写好,基本上就可以通用。 ExcelData excelData =...POI3.0 : 读取Excel文件
使用了poi包,实现xlsx文件内容读取,写入。程序需要maven环境,运行起来后是swing,实现了对内容的重排序,模板在\src\model下
它是一个简单高效的java工具,用于读取、写入和修改XLSX。 对其进行编码的最重要目的是出于性能考虑——像 POI 这样的所有流行代码在内存消耗和解析/写入速度方面都很糟糕。 记忆 sjxlsx 提供两种模式(经典和流)...
利用Java swing 对csv和excel文件进行读写;并且可以同时读取5个文件的数量;通过关键字配置来读文件,还有就是通过POI CSVWriter等类的使用来对excel和csv等文件进行操作
让你专注于数据,而不是文件格式 支持项目 如果贵公司已将 pyexcel 及其组件嵌入到创收产品中,请在 github 上支持我,或维护该项目并进一步开发。 如果您是个人,也欢迎您支持我,无论您喜欢多久。 作为我的支持者...
用于excel表格(xls、xlsx)读取/写入数据
读取模式包含用户模式和事件驱动模式 ,事件驱动模式能够支持大数据量的读操作,写操作xlsx使用sxssf方式支持大数据量的写入操作。 demo 基于poi 3.10.1版本 jdk为1.7 可直接导入ecplise 测试类为TestSrv.java。 ...
java实现在Excel中插入图片有时碰到下载excel中加入图片或已经制作好的图章。
一个R包,用于读取,写入,格式化Excel 2007和Excel 97/2000 / XP / 2003文件 该软件包提供R函数来读取,写入和格式化Excel文件。 它取决于Java,但这使它在大多数操作系统上都可用。 安装 来自CRAN的稳定版本 ...
与 Excel Streaming Reader 如果您过去使用过来读取Excel文件,您可能会注意到... 注意:此库仅支持读取XLSX文件。 有关Java 7支持的重要通知 该库的最新版本(2.x)放弃了对Java 7的支持。这是由于POI 4.0要求使用Jav