数据的导入导出
一、供应商或客户数据导出
1、需求分析
点击导出按钮,将供应商或客户的信息导出为excel文档
2、POI介绍
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“可怜的模糊实现”。
用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)
结构:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。
3、添加POI依赖,在erp_partent下的pom.xml当中
4、在erp_biz下的ISupplierBiz当中
5、修改erp_entity下的Supplier
6、在erp_biz下的ISupplierBiz的实现类SupplierBiz
package com.itzheng.erp.biz.impl;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.itzheng.erp.biz.ISupplierBiz;
import com.itzheng.erp.dao.ISupplierDao;
import com.itzheng.erp.entity.Supplier;
/**
* 供应商业务逻辑类
* @author Administrator
*
*/
public class SupplierBiz extends BaseBiz<Supplier> implements ISupplierBiz {
private ISupplierDao supplierDao;
public void setSupplierDao(ISupplierDao supplierDao) {
this.supplierDao = supplierDao;
setBaseDao(supplierDao);
}
/**
* 导出数据
*/
public void export(OutputStream os, Supplier t1) {
//获取要导出的数据列表
List<Supplier> list = supplierDao.getList(t1, null, null);
//创建一个工作簿
HSSFWorkbook wb = new HSSFWorkbook();
String sheetName = "";
if(Supplier.TYPE_CUSTOMER.equals(t1.getType())){
sheetName = "客户";
}
if(Supplier.TYPE_SUPPLIER.equals(t1.getType())){
sheetName = "供应商";
}
//创建一个工作表
HSSFSheet sheet = wb.createSheet(sheetName);
//创建一行,行的索引是从0开始, 写标题
HSSFRow row = sheet.createRow(0);
String[] header = {"名称","地址","联系人","电话","Email"};
int[] width = {5000,8000,4000,8000,10000};
HSSFCell cell = null;
for(int i = 0; i < header.length; i++){
cell = row.createCell(i);
cell.setCellValue(header[i]);
//设置列宽
sheet.setColumnWidth(i, width[i]);
}
//导出的内容
int rowCount = 1;
for(Supplier supplier : list){
row = sheet.createRow(rowCount);
row.createCell(0).setCellValue(supplier.getName());//名称
row.createCell(1).setCellValue(supplier.getAddress());//地址
row.createCell(2).setCellValue(supplier.getContact());//联系人
row.createCell(3).setCellValue(supplier.getTele());//电话
row.createCell(4).setCellValue(supplier.getEmail());//Email
rowCount++;
}
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
7、 在erp_web下的SupplierAction当中
/**
* 导出数据
*/
public void export(){
String filename = "";
if(Supplier.TYPE_SUPPLIER.equals(getT1().getType())){
filename = "供应商";
}
if(Supplier.TYPE_CUSTOMER.equals(getT1().getType())){
filename = "客户";
}
filename += ".xls";
//响应对象
HttpServletResponse response = ServletActionContext.getResponse();
try {
//设置输出流,实现下载文件
response.setHeader("Content-Disposition", "attachment;filename=" +
new String(filename.getBytes(),"ISO-8859-1"));
supplierBiz.export(response.getOutputStream(), getT1());
} catch (IOException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
8、设置列宽
9、前端的实现
1)创建download.js
在erp_web下的src当中的webapp/ui当中创建download.js
// Ajax 文件下载
$.download = function(url, data){ // 获得url和data
var inputs = '';
$.each(data, function(name, value) {
inputs+='+ name +'" value="'+ value +'" />';
});
$(' url +'" method="post">'+inputs+'')
.appendTo('body').submit().remove();
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2)在erp_web下的src当中的webapp/ui当中supplier.html当中引入download.js
3)修改crud.js
二、导出订单
1、导出订单后端biz
(1)在erp_biz下的IOrdersBiz当中
(2)IOrdersBiz的实现类当中OrdersBiz
@Override
public void export(OutputStream os, Long uuid) {
// 创建一个工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 获取订单
Orders orders = ordersDao.get(uuid);
List<Orderdetail> detaillist = orders.getOrderDetails();
// 创建工作表
String sheetName = "";
if (Orders.TYPE_IN.equals(orders.getType())) {
sheetName = "采购单";
}
if (Orders.TYPE_OUT.equals(orders.getType())) {
sheetName = "销售单";
}
HSSFSheet sheet = wb.createSheet(sheetName);
// 创建一行,行的索引是从0开始
HSSFRow row = sheet.createRow(0);
// 创建单元格,列的索引是从0 开始
HSSFCell cell = row.createCell(0);
// 创建单元格样式
HSSFCellStyle style_content = wb.createCellStyle();
style_content.setBorderBottom(BorderStyle.THIN);// 下边框
style_content.setBorderTop(BorderStyle.THIN);// 上边框
style_content.setBorderLeft(BorderStyle.THIN);// 左边框
style_content.setBorderRight(BorderStyle.THIN);// 右边框
// 设置水平对其方式为居中
style_content.setAlignment(HorizontalAlignment.CENTER);
// 设置垂直对其方式为居中
style_content.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置日期格式
HSSFCellStyle style_date = wb.createCellStyle();
// 把style_context里面样式复制到style_date
style_date.cloneStyleFrom(style_content);
DataFormat df = wb.createDataFormat();
style_date.setDataFormat(df.getFormat("yyyy-MM-dd HH:mm:ss"));
// 设置标题的样式
HSSFCellStyle style_title = wb.createCellStyle();
style_title.setAlignment(HorizontalAlignment.CENTER);
style_title.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont style_font = wb.createFont();
style_font.setFontName("黑体");
style_font.setFontHeightInPoints((short) 18);
// 加粗
style_font.setBold(true);
style_title.setFont(style_font);
// 创建内容样式的字体
HSSFFont font_content = wb.createFont();
// 设置字体名称,相当于选中了那种字符
font_content.setFontName("宋体");
// 设置字体的大小
font_content.setFontHeightInPoints((short) 11);
style_content.setFont(font_content);
// 合并单元格
// 合并:标题
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
// 合并第二行
sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 3));
// 合并第7行
sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 3));
// 创建11行,4列
// 创建矩阵11行,4列
int rowCount = detaillist.size() + 9;// 创建单元格的数量是数据的长度+9
for (int i = 2; i <= rowCount; i++) {
row = sheet.createRow(i);
for (int j = 0; j < 4; j++) {
// 给单元格设置样式
row.createCell(j).setCellStyle(style_content);
}
}
// 必须先有创建的行和单元格,才可以使用
// 创建标题单元格
HSSFCell titleCell = sheet.createRow(0).createCell(0);
titleCell.setCellValue(sheetName);
// 设置标题样式
titleCell.setCellStyle(style_title);
sheet.getRow(2).getCell(0).setCellValue("供应商");
sheet.getRow(3).getCell(0).setCellValue("下单日期");
sheet.getRow(4).getCell(0).setCellValue("审核日期");
sheet.getRow(5).getCell(0).setCellValue("采购日期");
sheet.getRow(6).getCell(0).setCellValue("入库日期 ");
sheet.getRow(3).getCell(2).setCellValue("经办人");
sheet.getRow(4).getCell(2).setCellValue("经办人");
sheet.getRow(5).getCell(2).setCellValue("经办人");
sheet.getRow(6).getCell(2).setCellValue("经办人");
sheet.getRow(7).getCell(0).setCellValue("订单明细");
sheet.getRow(8).getCell(0).setCellValue("商品名称");
sheet.getRow(8).getCell(1).setCellValue("商品数量");
sheet.getRow(8).getCell(2).setCellValue("商品价格");
sheet.getRow(8).getCell(3).setCellValue("金额");
// 设置行高于列宽
// 标题行高
sheet.getRow(0).setHeight((short) 1000);
// 内容体的行高
for (int i = 2; i <= rowCount; i++) {
sheet.getRow(i).setHeight((short) 500);
}
// 设置列宽
for (int i = 0; i < 4; i++) {
sheet.setColumnWidth(i, (short)