excle下载
public String downloadexcel(HttpServletRequest request,HttpServletresponse response,String name) {
outputstream os = null;
WritableWorkbook wbook = null;
try {
String flieName = name;
// 取得输出流
os = response.getOutputStream();
response.reset();// 清空输出流
// 设定输出文件头
response.setheader("content-Disposition", "attachment; filename=" + new String(flieName.getBytes("UTF-8"), "ISO8859-1") + ".xls");
// 定义输出类型
response.setcontenttype("application/msexcel;charset=utf-8");
// 建立excel文件
wbook = Workbook.createWorkbook(os);
//要下载的list数据
List<UnifiedVO> unifiedVOList = new ArrayList<UnifiedVO>();
// 设置数据
setWorkBookData(unifiedVOList, wbook,flieName);
if (wbook.getNumberOfSheets() > 0) {
wbook.write();
}
wbook.close();
os.close();
} catch (Exception e) {
e.printstacktrace();
}finally {
if(wbook != null) {
try {
wbook.close();
} catch (WriteException | IOException e) {
e.printStackTrace();
}
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
String [] codes 是页面传过来的要查询的内容,name 是传过来动态表名。for循环查出List数据,然后统一用unifiedVOList.addAll()合在一起。然后将list数据集合传入方法内、work、表名传入方法内。
private void setWorkBookData(List<UnifiedVO> giftRecords, WritableWorkbook wbook,String name)
throws WriteException, RowsExceededException {
final integer EXCEL_MAX_NUMBER = 65535;
WritableCellformat wc = new WritableCellFormat();
// 设置居中
wc.setAlignment(Alignment.CENTRE);
simpledateformat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
WritableSheet wsheet = null;
int sheetNumber = 0;
if (giftRecords.size() % EXCEL_MAX_NUMBER != 0) {
sheetNumber = giftRecords.size() / EXCEL_MAX_NUMBER + 1;
} else {
sheetNumber = giftRecords.size() / EXCEL_MAX_NUMBER;
}
for (int j = 0; j < sheetNumber; j++) {
wsheet = wbook.createSheet(name + (j + 1), j); // sheet名称
// 开始生成主体内容
wsheet.addCell(new Label(0, 0, "名称", wc));
wsheet.setColumnView(0, 32);
wsheet.addCell(new Label(1, 0, "时间", wc));
wsheet.setColumnView(1, 20);
wsheet.addCell(new Label(2, 0, "数据", wc));
wsheet.setColumnView(2, 32);
wsheet.addCell(new Label(3, 0, "单位", wc));
wsheet.setColumnView(3, 30);
int start = EXCEL_MAX_NUMBER * j;
int end = EXCEL_MAX_NUMBER + start;
for (int i = start, k = giftRecords.size(); i < k && i < end; i++) {
UnifiedVO unifiedVO = giftRecords.get(i);
int row = i - start + 1;
wsheet.addCell(new Label(0, row, unifiedVO .getKey(), wc));
wsheet.addCell(new Label(1, row, unifiedVO .getDate(), wc));
wsheet.addCell(new Label(2, row, unifiedVO .getData(), wc));
wsheet.addCell(new Label(3, row, unifiedVO .getUnit(), wc));
}
}
}
wsheet.addCell(new Label(0, 0, "名称", wc))
wsheet.addCell(new Label(1, 0, "时间", wc));
wsheet.addCell(new Label(2, 0, "数据", wc));
wsheet.addCell(new Label(3, 0, "单位", wc));
上面的字段一一对应VO字段
wsheet.addCell(new Label(0, row, unifiedVO .getKey(), wc));
wsheet.addCell(new Label(1, row, unifiedVO .getDate(), wc));
wsheet.addCell(new Label(2, row, unifiedVO .getData(), wc));
wsheet.addCell(new Label(3, row, unifiedVO .getUnit(), wc));
对应的maven API :
<!-- 导出excel jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
=====================================================================================
poi 导出:
<!-- 导出excel poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
最简单的下载数据实现:
public test(HttpServletRequest request,HttpServletResponse response) {
List<?> arrayList = new ArrayList<Object>();
OutputStream os = null;
// 取得输出流
try {
os = response.getOutputStream();
} catch (IOException e2) {
e2.printStackTrace();
}
response.reset();// 清空输出流
//1.在内存中创建一个excel文件
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//2.创建工作簿
HSSFSheet sheet = hssfWorkbook.createSheet();
//3.创建标题行
HSSFRow titlerRow = sheet.createRow(0);
HSSFCellStyle createCellStyle = hssfWorkbook.createCellStyle();//创建单元格样式
createCellStyle.setFillForegroundcolor((short) 255);// 设置背景色
createCellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION); // 居中
createCellStyle.setBorderTop(BorderStyle.SLANTED_DASH_DOT);
titlerRow.createCell(0).setCellValue("营销顾问");
titlerRow.setRowStyle(createCellStyle);
for (Object o : arrayList) {
//获取最后一行的行号
int lastrownum = sheet.getLastRowNum();
HSSFRow dataRow = sheet.createRow(lastRowNum + 1);
dataRow.createCell(0).setCellValue(o.get营销顾问());
}
//5.创建文件名
String fileName = "区域数据统计.xls";
//9.设置信息头
response.setContentType(fileName);
try {
fileName = new String (fileName.getBytes ("utf-8"),"ISO8859-1");
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
response.setHeader("Content-Disposition","attachment;filename="+fileName);
//10.写出文件,关闭流
try {
hssfWorkbook.write(os);
} catch (IOException e1) {
}
try {
hssfWorkbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
相关阅读
Java SE Development Kit 6u41 Product / File Description File Size Download password Linux x86 65.43 MB jdk-6u41-
注:(fpSpread1为farpoint名,fpSpread1_Sheet1为表名) 用farpoint的时,设置视区随滚动条移动(鼠标按着,farpoint的内容更新到滚动条的区域
1:简单的截屏——截全屏 package com.auto.Test; import java.awt.Dimension; import java.awt.Rectangle;
关于java集合类Collections.在前端开发中可能用到的比较少,不过java后台用到的应该挺多,比较后台主要是基于数据库的读写的,对于并发
在多线程的场景中,我们需要保证数据安全,就会考虑同步的方案,通常会使用synchronized或者lock来处理,使用了synchronized意味着内核