1, create an ExcelExportAction class
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import org.apache.poi.hssf.util.HSSFColor;
import com.opensymphony.xwork2.ActionSupport;
public class ExcelExportAction extends ActionSupport{
private static final long serialVersionUID = 4565154051377016557L;
// private static final long cen = 2565374152L;
Private static final String[] header = { "serial number", "employee number", "employee name", "affiliation department", "position", "job date", "remark" };
private String excelFileName;
private InputStream excelStream;
/**
* export excel
*
* @return
*/
public String exportExcel() {
try {
// Create excel
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("test file 1");
// Here is createRow(int), don't remember with sheet.getRow(int). . . .
HSSFRow row = sheet.createRow(0);
row.setHeightInPoints(25);
HSSFCellStyle cellstyle = workbook.createCellStyle();
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellstyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cellstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
HSSFCell cell;
for (int i = 0; i < header.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(cellstyle);
sheet.setColumnWidth(i,25*256);
cell.setCellValue(header[i]);
}
// Define 20 rows of data to be inserted into the excel table
for (int j = 1; j <= 50; j++) {
row = sheet.createRow(j);
row.setHeightInPoints(20);
HSSFCell cell0 = row.createCell(0);
cell0.setCellValue(j);
HSSFCell cell1 = row.createCell(1);
cell1.setCellValue("employee number" + j);
HSSFCell cell2 = row.createCell(2);
cell2.setCellValue("employee name" + j);
HSSFCell cell3 = row.createCell(3);
cell3.setCellValue("Soft Research" + j + "Parts");
HSSFCell cell4 = row.createCell(4);
cell4.setCellValue("programmer" + j);
HSSFCell cell5 = row.createCell(5);
cell5.setCellValue("2013-05-28");
HSSFCell cell6 = row.createCell(6);
cell6.setCellValue("remark");
}
// save the file in the stream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
byte[] fileContent = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(fileContent);
excelStream = bis;
excelFileName = "EXCEL file export test.xls";
excelFileName = new String(excelFileName.getBytes(),"iso-8859-1");
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
/** getter/setter method **/
public String getExcelFileName() {
return excelFileName;
}
public void setExcelFileName(String excelFileName) {
this.excelFileName = excelFileName;
}
public InputStream getExcelStream() {
return excelStream;
}
public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}
}
2, struts.xml configuration
<!-- exprot excel file -->
<action name="exportExcel" class="com.hnyyzw.trm.action.ExcelExportAction"
method="exportExcel">
<result name="success" type="stream">
<!-- Download file type -->
<param name="contentType">application/vnd.ms-excel</param>
<!-- return stream excelStream variable name in Action -->
<param name="inputName">excelStream</param>
<!-- attachment The parameters of this location are quite special. You can set whether to download a prompt box when downloading, or download it directly. fileName specifies the name of the generated file (suitable for dynamically generating file names, such as when reporting, generally speaking, it is a few months of statistical data) for the action variable -->
<param name="contentDisposition">attachment;filename=${excelFileName}</param>
<param name="bufferSize">1024</param>
</result>
</action>