package com.elemesoft.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.elemesoft.entity.Sysuser;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFCell;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFCellStyle;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFDataFormat;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFFont;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFRow;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFSheet;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelAction {
private InputStream excelFile;
private String downloadFileName;
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}
public String getDownloadFileName() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd ");
String downloadFileName = (sf.format(new Date()).toString())+ "项目信息.xls";
try {
downloadFileName = new String(downloadFileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
//这是要操作的实体类列表(一般是从数据库中查询的,方便起见,这里手写出来需要的数据)
public List<Sysuser> getUser(){
List<Sysuser> list = new ArrayList<Sysuser>();
Sysuser user1 = new Sysuser();
user1.setAccount("admin");
user1.setName("cp");
Sysuser user2 = new Sysuser();
user2.setAccount("guest");
user2.setName("tk");
list.add(user1);
list.add(user2);
return list;
}
//前台调用的action方法
public String export() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
List<Sysuser> dataList = getUser(); ///?????
HSSFWorkbook workbook = exportExcel(dataList);
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
return "excel";
}
public HSSFWorkbook exportExcel(List<Sysuser> dataList) throws Exception {
HSSFWorkbook workbook = null;
// 创建工作簿实例
try {
workbook = new HSSFWorkbook();
// 创建工作表实例
HSSFSheet sheet = workbook.createSheet("TscExcel");
// 设置列宽
this.setSheetColumnWidth(sheet);
// 获取样式
HSSFCellStyle style = this.createTitleStyle(workbook);
if (dataList != null && dataList.size() > 0) {
// 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
HSSFRow row = sheet.createRow((short) 0);// 建立新行
//这里是第一行,一般用来对excel中的数据进行说明(copy的话记得改这里)
this.createCell(row, 0, style, HSSFCell.CELL_TYPE_STRING, "序号");
this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING, "终端名称");
this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING, "手机号码");
// 给excel填充数据
for (int i = 0; i < dataList.size(); i++) {
// 将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦
Sysuser model = (Sysuser) dataList.get(i);
HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING,i + 1);
//这里是excel中要显示的数据,根据你自己的实体类(copy的话记得改这里)
if (model.getName() != null)
this.createCell(row1, 1, style,HSSFCell.CELL_TYPE_STRING, model.getName());
if (model.getAccount() != null)
this.createCell(row1, 2, style,HSSFCell.CELL_TYPE_STRING, model.getAccount());
}
} else {
this.createCell(sheet.createRow(0), 0, style,HSSFCell.CELL_TYPE_STRING, "查无资料");
}
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
private void setSheetColumnWidth(HSSFSheet sheet) {
// 根据你数据里面的记录有多少列,就设置多少列(我用的poi需要些short,高版本的貌似不用,你可以试试)
sheet.setColumnWidth((short)0, (short)3000);
sheet.setColumnWidth((short)1, (short)8000);
}
// 设置excel的title样式
private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFFont boldFont = wb.createFont();
boldFont.setFontHeight((short) 200);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
return style;
}
// 创建Excel单元格
private void createCell(HSSFRow row, int column, HSSFCellStyle style,int cellType, Object value) {
HSSFCell cell = row.createCell((short) column);
if (style != null) {
cell.setCellStyle(style);
}
switch (cellType) {
case HSSFCell.CELL_TYPE_BLANK: {
}
break;
case HSSFCell.CELL_TYPE_STRING: {
cell.setCellValue(value.toString());
}
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.parseDouble(value.toString()));
}
break;
default:
break;
}
}
}
以上是action中的方法,需要修改的地方注释中有说明,我是遍历的List<Sysuser>对象。
<result name="excel" type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="bufferSize">1024</param> <param name="inputName">excelFile</param> </result
struts的xml
然后前台直接<a href="action.....">下载</a>就可以了