excel文件导出
2016-07-27 16:36
309 查看
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;
/**
* 供应商导出
*
* @param request
* @param response
* @return
* @throws Exception
*/
@SecurityMapping(title = "supplier_export.htm供应商导出", value = "/erp/supplier_export.htm*", rtype = "seller", rname = "supplier供应商导出", rcode = "supplier_user", display = false, rgroup = "supplier管理")
@RequestMapping("/erp/supplier_export.htm")
@ResponseBody
public void supplier_export(HttpServletRequest request, HttpServletResponse response, String ids) throws Exception {
Long userId = SecurityUserHolder.getConditionId();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet JobSheet = workbook.createSheet("供应商数据");
this.supplierService.produceJobSheet(workbook, JobSheet, userId, ids);// 有测试数据提交小心
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String fileName = "供应商数据" + sdf.format(date) + ".xls";
// 执行文件输出
this.runExcelFileExport(response, workbook, fileName);
}
/**
* 创建excel2003
*
* @param response
* @param wb
* @param fileName
* 文件名
*/
public void runExcelFileExport(HttpServletResponse response, HSSFWorkbook wb, String fileName) {
try {
ServletOutputStream sos = response.getOutputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
wb.write(buffer);
response.setContentType("application/vnd.ms-excel");
response.setContentLength(buffer.size());
response.setHeader("Content-Disposition",
"attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");
sos.write(buffer.toByteArray());
buffer.flush();
sos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建excel
*
* @param workbook
* @param sheet
* @throws Exception
*/
@Override
public void produceJobSheet(HSSFWorkbook workbook, HSSFSheet sheet, Long userId, String ids) throws Exception {
HSSFRow row1 = sheet.createRow(0);// 创建行1
sheet.setColumnWidth(0, 6000);// 设置列宽
sheet.setColumnWidth(1, 6000);// 设置列宽
sheet.setColumnWidth(2, 6000);// 设置列宽
sheet.setColumnWidth(3, 6000);// 设置列宽
sheet.setColumnWidth(4, 6000);// 设置列宽
sheet.setColumnWidth(5, 6000);// 设置列宽
HSSFCell code1 = row1.createCell(0);
code1.setCellValue("供应商编号");
HSSFCell sequence1 = row1.createCell(1);
sequence1.setCellValue("供应商序号");
HSSFCell name1 = row1.createCell(2);
name1.setCellValue("供应商名称");
HSSFCell contacts1 = row1.createCell(3);
contacts1.setCellValue("联系人");
HSSFCell phoneNum1 = row1.createCell(4);
phoneNum1.setCellValue("联系电话");
HSSFCell address1 = row1.createCell(5);
address1.setCellValue("地址信息");
List<Supplier> supplier = this.supplierDao.produceJobSheet(userId, ids);
int i = 1;
for (Supplier su : supplier) {
HSSFRow row = sheet.createRow(i);// 创建行1
sheet.setColumnWidth(0, 6000);// 设置列宽
sheet.setColumnWidth(1, 6000);// 设置列宽
sheet.setColumnWidth(2, 6000);// 设置列宽
sheet.setColumnWidth(3, 6000);// 设置列宽
sheet.setColumnWidth(4, 6000);// 设置列宽
sheet.setColumnWidth(5, 6000);// 设置列宽
HSSFCell code = row.createCell(0);
code.setCellValue(su.getCode());
HSSFCell sequence = row.createCell(1);
sequence.setCellValue(su.getSequence());
HSSFCell name = row.createCell(2);
name.setCellValue(su.getName());
HSSFCell contacts = row.createCell(3);
contacts.setCellValue(su.getContacts());
HSSFCell phoneNum = row.createCell(4);
phoneNum.setCellValue(su.getPhoneNum());
HSSFCell address = row.createCell(5);
address.setCellValue(su.getAddress());
i++;
}
}
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* 供应商导出
*
* @param request
* @param response
* @return
* @throws Exception
*/
@SecurityMapping(title = "supplier_export.htm供应商导出", value = "/erp/supplier_export.htm*", rtype = "seller", rname = "supplier供应商导出", rcode = "supplier_user", display = false, rgroup = "supplier管理")
@RequestMapping("/erp/supplier_export.htm")
@ResponseBody
public void supplier_export(HttpServletRequest request, HttpServletResponse response, String ids) throws Exception {
Long userId = SecurityUserHolder.getConditionId();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet JobSheet = workbook.createSheet("供应商数据");
this.supplierService.produceJobSheet(workbook, JobSheet, userId, ids);// 有测试数据提交小心
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String fileName = "供应商数据" + sdf.format(date) + ".xls";
// 执行文件输出
this.runExcelFileExport(response, workbook, fileName);
}
/**
* 创建excel2003
*
* @param response
* @param wb
* @param fileName
* 文件名
*/
public void runExcelFileExport(HttpServletResponse response, HSSFWorkbook wb, String fileName) {
try {
ServletOutputStream sos = response.getOutputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
wb.write(buffer);
response.setContentType("application/vnd.ms-excel");
response.setContentLength(buffer.size());
response.setHeader("Content-Disposition",
"attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");
sos.write(buffer.toByteArray());
buffer.flush();
sos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建excel
*
* @param workbook
* @param sheet
* @throws Exception
*/
@Override
public void produceJobSheet(HSSFWorkbook workbook, HSSFSheet sheet, Long userId, String ids) throws Exception {
HSSFRow row1 = sheet.createRow(0);// 创建行1
sheet.setColumnWidth(0, 6000);// 设置列宽
sheet.setColumnWidth(1, 6000);// 设置列宽
sheet.setColumnWidth(2, 6000);// 设置列宽
sheet.setColumnWidth(3, 6000);// 设置列宽
sheet.setColumnWidth(4, 6000);// 设置列宽
sheet.setColumnWidth(5, 6000);// 设置列宽
HSSFCell code1 = row1.createCell(0);
code1.setCellValue("供应商编号");
HSSFCell sequence1 = row1.createCell(1);
sequence1.setCellValue("供应商序号");
HSSFCell name1 = row1.createCell(2);
name1.setCellValue("供应商名称");
HSSFCell contacts1 = row1.createCell(3);
contacts1.setCellValue("联系人");
HSSFCell phoneNum1 = row1.createCell(4);
phoneNum1.setCellValue("联系电话");
HSSFCell address1 = row1.createCell(5);
address1.setCellValue("地址信息");
List<Supplier> supplier = this.supplierDao.produceJobSheet(userId, ids);
int i = 1;
for (Supplier su : supplier) {
HSSFRow row = sheet.createRow(i);// 创建行1
sheet.setColumnWidth(0, 6000);// 设置列宽
sheet.setColumnWidth(1, 6000);// 设置列宽
sheet.setColumnWidth(2, 6000);// 设置列宽
sheet.setColumnWidth(3, 6000);// 设置列宽
sheet.setColumnWidth(4, 6000);// 设置列宽
sheet.setColumnWidth(5, 6000);// 设置列宽
HSSFCell code = row.createCell(0);
code.setCellValue(su.getCode());
HSSFCell sequence = row.createCell(1);
sequence.setCellValue(su.getSequence());
HSSFCell name = row.createCell(2);
name.setCellValue(su.getName());
HSSFCell contacts = row.createCell(3);
contacts.setCellValue(su.getContacts());
HSSFCell phoneNum = row.createCell(4);
phoneNum.setCellValue(su.getPhoneNum());
HSSFCell address = row.createCell(5);
address.setCellValue(su.getAddress());
i++;
}
}
相关文章推荐
- HDU5727
- Codeforces Round #244 (Div. 2) E. Police Patrol(数学)
- Android梳理不常用widget篇
- js 生成二维码实例
- mysql -- 区分apk包中,java包,android包,第三方包的方法
- 实体引用
- 【转载】十条jQuery代码片段助力Web开发效率提升
- 看慕课节日祝福总结
- UVA 12105 Bigger is Better(数位DP)
- 六、数据库优化(上)——Mysql主从复制搭建
- GridView布局,自定义适配器,水平滚动
- 使用Lombok简化你的代码
- 一致性hash算法学习
- 拖拽回放
- [置顶] android 底层日志 开发框架 崩溃后进行记录 LogReport
- 使用NodeJS调用Dubbo工程
- Android触摸屏事件派发机制详解与源码分析二(ViewGroup篇)
- ListView
- hdu5754Life Winner Bo+博弈
- XZ_HTML5之HTML脚本、实体和URL