jxl excelUtils


package com.ruijie.dinnerorder.web.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelUtils {
private static String sheetName = "data";
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
private HSSFCell cell;
private HSSFFont font;
private HSSFCellStyle cellStyle;
private FileOutputStream fileOut;
public ExcelUtils() {
wb = new HSSFWorkbook();
}

/**
* @param excelName
* excel名称。
* @param list
* 这个list里面存放的是对象数组。数组元素可以转化为字符串显示的。这个对象数组一般对应数据库里的几列。
* @param firstRowValue
* */
public void outputExcel(String excelName, List list, String[] firstRowValue) {
try {
this.createSheet(firstRowValue);
this.setValueToRow(excelName, list);
} catch (Exception ex) {
System.out.print(ex);
} // System.out.println("文件名是:" + excelName); downloadFile(excelName); }
}

public void outputExcel(String excelName, List list) {
try {
this.setValueToRow(excelName, list);
} catch (Exception e) {
// TODO: handle exception
}
downloadFile(excelName);
}

/**
* 设置excel的没一行的值...
* @param excelName
* @param list
*/
private void setValueToRow(String excelName, List list) {
// 获得JSF上下文环境
FacesContext context = FacesContext.getCurrentInstance();
// 获得ServletContext对象
ServletContext servletContext = (ServletContext) context
.getExternalContext().getContext();
// 取得文件的绝对路径
excelName = servletContext.getRealPath("/UploadFile") + "/" + excelName;
System.out.println("生成文件的路径是:" + excelName);
Object[] obj;
try {
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
obj = (Object[]) list.get(i);
this.createCell(row, obj);
}
fileOut = new FileOutputStream(excelName);
wb.write(fileOut);

} catch (Exception ex) {
System.out.print("生成报表有误:" + ex);
} finally {
try {
fileOut.flush();
fileOut.close();
} catch (Exception e) {
System.out.println("ExcelUtil.setValueToRow()");
}
}
}

/**
* 创建表头
* @param firstRowValue
*/
private void createSheet(String[] firstRowValue) {
try {
sheet = wb.createSheet(ExcelUtils.sheetName);
row = sheet.createRow(0);
font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
for (int i = 0; i < firstRowValue.length; i++) {
cell = row.createCell((short) i);
cell.setCellStyle(cellStyle);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(firstRowValue[i]);
}
} catch (Exception ex) {
System.out.print(ex);
}
}

/**
* 创建表格的值
* @param row
* @param obj
*/
private void createCell(HSSFRow row, Object[] obj) {
try {
for (int i = 0; i < obj.length; i++) {
cell = row.createCell((short) i);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(obj[i].toString());
}
} catch (Exception ex) {
System.out.print(ex);
}
}

/**
* <p>
* 功能说明:根据提供的文件名下载文件,不支持中文文件名
* </p>
* 此方法由yongtree添加,实现文件生成后的下载
*
* @param strfileName
* String
* @return void
*/
private static void downloadFile(String strfileName) {
try {
// 获得JSF上下文环境
FacesContext context = FacesContext.getCurrentInstance();
// 获得ServletContext对象
ServletContext servletContext = (ServletContext) context
.getExternalContext().getContext();
// 取得文件的绝对路径
String excelName = servletContext.getRealPath("/UploadFile") + "/"
+ strfileName;
File exportFile = new File(excelName);

HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
ServletOutputStream servletOutputStream = httpServletResponse
.getOutputStream();
httpServletResponse.setHeader("Content-disposition",
"attachment; filename=" + strfileName);
httpServletResponse.setContentLength((int) exportFile.length());
httpServletResponse.setContentType("application/x-download");
// httpServletResponse.setContentType("application/vnd.ms-excel");

byte[] b = new byte[1024];
int i = 0;
FileInputStream fis = new java.io.FileInputStream(exportFile);
while ((i = fis.read(b)) > 0) {
servletOutputStream.write(b, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
}
FacesContext.getCurrentInstance().responseComplete();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值