Java实现导出Excel文件
2016-05-18 22:47
381 查看
/**
* <p>
* Discription:[导出excel]
* </p>
*
* @param response
* @param list 要导出的数据集合
* @param fileName
* @param sheetName
* @throws Exception
*/
public static void exportXls(HttpServletResponse response, List<String[]> list, String fileName,
String sheetName) throws Exception
{
if (list == null || list.size() == 0)
{
return;
}
// 获取总行数
int rowNum = list.size();
// 获得总列数
int cellNum = list.get(0).length;
// 创建Excel文档
HSSFWorkbook hwb = new HSSFWorkbook();
// sheet对应一个工作页
HSSFSheet sheet = hwb.createSheet(sheetName);
for (int i = 0; i < rowNum; i++)
{
// 创建一行
HSSFRow row = sheet.createRow(i);
// 插入一行数据
for (int j = 0; j < cellNum; j++)
{
HSSFCell cell = row.createCell(j);
cell.setCellValue(list.get(i)[j]);
}
}
// 创建文件输出流,准备输出电子表格
ByteArrayOutputStream os = new ByteArrayOutputStream();
hwb.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
}
} catch (IOException e)
{
throw e;
} finally
{
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
* <p>
* Discription:[导出excel]
* </p>
*
* @param response
* @param list 要导出的数据集合
* @param fileName
* @param sheetName
* @throws Exception
*/
public static void exportXls(HttpServletResponse response, List<String[]> list, String fileName,
String sheetName) throws Exception
{
if (list == null || list.size() == 0)
{
return;
}
// 获取总行数
int rowNum = list.size();
// 获得总列数
int cellNum = list.get(0).length;
// 创建Excel文档
HSSFWorkbook hwb = new HSSFWorkbook();
// sheet对应一个工作页
HSSFSheet sheet = hwb.createSheet(sheetName);
for (int i = 0; i < rowNum; i++)
{
// 创建一行
HSSFRow row = sheet.createRow(i);
// 插入一行数据
for (int j = 0; j < cellNum; j++)
{
HSSFCell cell = row.createCell(j);
cell.setCellValue(list.get(i)[j]);
}
}
// 创建文件输出流,准备输出电子表格
ByteArrayOutputStream os = new ByteArrayOutputStream();
hwb.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
}
} catch (IOException e)
{
throw e;
} finally
{
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
相关文章推荐
- JAVA设计模式(11):结构型-装饰模式(Decorator)
- Java文件读写操作
- Java环境搭建之Eclipse常用快捷键
- [原创] Windows下Eclipse连接hadoop
- Java HashMap的工作原理
- 内部类
- eclipse 添加.gitignore
- 【学习笔记】eclipse 编译正确,只能打印出208
- Java实现下载网上的图片到本地
- Struts2间接访问Servlet API(ActionContex--一般推荐使用)
- 电商系统的高并发设计和挑战
- javaWeb开发之(一)_jdk安装与配置(Windows)
- 关于java.util.Vector 或 java.util.Hashtable类过时的讨论
- java笔记之类和对象
- Java接口和抽象类
- MyEclipse优化设置
- Java使用quartz实现作业调度
- java中的Clone(深拷贝,浅拷贝)
- [原创]java WEB学习笔记10:GenericServlet
- Struts2_Action记录在线人数实例