package com.excel.test;
import java.io.*;
import java.util.List;
import javax.servlet.http.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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 com.wp.domain.Member;
import com.wp.service.MemberService;
/**
* 导出导入报表
* @author Administrator
*
*/
public class ExcelTest {
private static MemberService mbService;
//**********************************测试*******************************
/**
* 导出所有用户
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public String getExportOperator(HttpServletRequest request,
HttpServletResponse response) {
response.setHeader("Charset", "UTF-8");
response.setContentType("text/html; charset=utf-8");
List<Member> operList = mbService.getMemberList();
if (null != operList) {
Workbook wb = (Workbook) new XSSFWorkbook();
Sheet sheet = wb.createSheet("用户表");
sheet.setColumnWidth(0, 4000);
sheet.setColumnWidth(1, 4000);
sheet.setColumnWidth(2, 7000);
sheet.setColumnWidth(3, 4000);
sheet.setColumnWidth(4, 4000);
sheet.setColumnWidth(5, 4000);
sheet.setColumnWidth(6, 4000);
sheet.setColumnWidth(7, 4000);
sheet.setColumnWidth(8, 4000);
sheet.setColumnWidth(9, 4000);
sheet.setColumnWidth(10, 4000);
sheet.setColumnWidth(11, 4000);
sheet.setColumnWidth(12, 4000);
Row row = sheet.createRow((int) 0);
CellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//style.setAlignment(CellStyle.ALIGN_CENTER);
Cell cell = row.createCell((short) 0);
cell.setCellValue("用户名");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("真实姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("证件号码");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("联系电话");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("备注");
cell.setCellStyle(style);
for (int i = 0; i < operList.size(); i++) {
row = sheet.createRow((int) i + 1);
// 第四步,创建单元格,并设置值
Member oper = operList.get(i);
row.createCell((short) 0).setCellValue(oper.getMbUserName());
row.createCell((short) 1).setCellValue(oper.getMbRealName());
row.createCell((short) 2).setCellValue(oper.getMbIDCards());
row.createCell((short) 3).setCellValue(oper.getMbMobile());
row.createCell((short) 4).setCellValue("");
}
try {
File file = new File(request.getSession().getServletContext().getRealPath("/DeviceImport"));
if(!file.exists()){
try {
file.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
FileOutputStream fout = new FileOutputStream(file
+ "\\用户存根表.xlsx");
wb.write(fout);
fout.close();
GetDownloadExecl(file + "\\用户存根表.xlsx", request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public void GetDownloadExecl(String path, HttpServletRequest request,
HttpServletResponse response) {
try {
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1)
.toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("gb2312"), "ISO8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response
.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//**********************************测试*******************************
//********************导出 2007*************************************************
public void getWordBook(HttpServletRequest request) throws IOException{
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
Workbook xwb = new XSSFWorkbook(request.getSession().getServletContext().getRealPath("/DeviceImport")+"/ss.xlsx");
// 读取第一章表格内容
Sheet sheet = xwb.getSheetAt(0);
// 定义 row、cell
Row row;
String cell;
// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
// 通过 row.getCell(j).toString() 获取单元格内容,
cell = row.getCell(j).toString();
System.out.print(cell + "\t");
}
System.out.println("");
}
}
//********************导出 2007*******************************************************
}
相关推荐
本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...
EXCEL2007百宝箱,可以方便企业财务和办公室使用
Excel 2007 里面需要用到规划求解的宏。
本书正是这样一本带你逐步上手并精通Excel2007的使用手册,涵盖了Excel2007的所有新特性。全书共分为6个部分:第一部分让读者全面领略Excel2007的全新功能区用户界面;第二部分介绍了Excel2007的新特性,如大网格、...
EXCEL 2007 宝典 OFFICE2007 OFFICE EXCEL 2007 宝典 2/10 出版时间:2008-7-1 版 次:1页 数:701字 数:1274000 印刷时间:2008-7-1开 本:16开纸 张:胶版纸 印 次:1I S B N:9787121067204包 装:...
EXCEL 2007 宝典 OFFICE2007 OFFICE EXCEL 2007 宝典 7/10 出版时间:2008-7-1 版 次:1页 数:701字 数:1274000 印刷时间:2008-7-1开 本:16开纸 张:胶版纸 印 次:1I S B N:9787121067204包 装:...
Excel2007 与VBA编程 从入门到精通 OFFICE2007 原价:43.00元 作者:张强,刘飚 编著 出版社:电子工业出版社 出版日期:2008-2-1 ISBN:9787121057250 字数:750000 页码:457 内容提要 -----------------------...
Excel2007打开多个独立窗口的方法
Excel2007 已经问世很久了,但我发现身边有好多朋友依然在使用 Excel2003,询问之后得知有些人是因为 2007 版本页面变化太大,很多 功能找不到地方,导致自己越忙越乱最终而放弃了使用;还有一部分人是 因为根本...
本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...
本资源为PDF文档,详细介绍了excel2003到excel2007的大多操作指南,可以说是全部,以表格的形式罗列出来,很直观,简单明了已于初学者参照使用excel用于办公或各自所需。
《中文版Excel2007高效办公实例精讲》是专门介绍Excel使用方法与技巧的图书。Excel是Microsoft公司推出的Office系列产品中的一个重要组成部分,其最新版本为Excel2007。Excel是公司进行信息管理的重要工具之一。 ...
Microsoft Excel2007驱动程序AccessDatabaseEngine.exe,用于解决System.InvalidOperationException: 未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序
本书以全新的文字、图形和实例文件全面介绍了MicrosoftExcel2007的强大功能和使用方法。首先介绍Excel2007的新界面以及新增功能,让用户从整体上掌握Excel的基本知识。然后介绍了Excel中常用的公式和函数,以及如何...
Excel2007_创建自定义Excel加载项,2010通用
Word可以进行多种方式打印,如打印奇数页、打印偶数页、打印当前页等。而Excel的各种版本一直没有这些...单击“浏览”,查找并选择“Excel2007双面打印工具.xlam”文件,单击“确定”。 该加载宏仅适用于Excel 2007。
第2部分为第2章~第27章,通过大量的实例介绍了Excel的关键对象,并介绍了Excel 2007新的Office XML文件格式和RibbonX开发技术,其中一些章节专门介绍了为提取各种格式的数据而访问外部数据库的详细技术,最后4章...
Excel2003至Excel2007交互式命令参考指南