用java做了一个系统,现在希望用一个按钮,点击后生成excel报表:把查询信息生成到excel报表中

请问如何实现?需要具体一点的代码!!谢谢!!

strust2 实现的
jsp
<input type="button" class="button" onclick="importExcel()" value="导出Excel"/>function importExcel() {
var isConfirm = confirm("您要导出Excel?");
if (isConfirm) {
var url=encodeURI("printExcel.do");
self.location=url;
}
}
java:
public String printExcel() {
fileName = 文件名;
获取数据

long findCount = 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, "", HSSFWorkbook.ENCODING_UTF_16);
sheet.addMergedRegion(new Region(0, (short) (0), 0, (short) (9)));
HSSFRow row = sheet.createRow(0);
HSSFCell cell[] = new HSSFCell[13];
cell[0] = row.createCell((short) 0);
cell[0].setEncoding(HSSFCell.ENCODING_UTF_16);
cell[0].setCellValue(文件名);
cell[0].setEncoding(HSSFCell.ENCODING_UTF_16);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell[0].setCellStyle(style);

HSSFRow row1 = sheet.createRow(1);
HSSFCell cell1[] = new HSSFCell[18];
for (short i = 0; i < 10; i++) {
cell1[i] = row1.createCell(i);
cell1[i].setEncoding(HSSFCell.ENCODING_UTF_16);
}
cell1[0].setCellValue("姓名");
cell1[0].setCellStyle(style);

int len = 数据长度;
for (int i = 0; i < len; i++) {

Villager deviceFees = 数据.get(i);

HSSFRow row3 = sheet.createRow(i + 2);
HSSFCell cell3[] = new HSSFCell[18];

for (short j = 0; j < 15; j++) {
cell3[j] = row3.createCell(j);
cell3[j].setEncoding(HSSFCell.ENCODING_UTF_16);
}
cell3[0].setCellValue(deviceFees.getName());
findCount++;
}

try {
wb.write(baos);
} catch (IOException e) {
e.printStackTrace();
}

try {
fileName = new String(fileName.getBytes("GBK"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

byte[] ba = baos.toByteArray();

this.setExcelFile(new ByteArrayInputStream(ba));
return SUCCESS;
}
xml
<action name="printExcel" class="action名字" method="action中的方法">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel;charset=ISO8859-1</param>
<param name="inputName">excelFile</param>
<param name="contentDisposition">attachment;filename="${fileName}.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-21
在网上搜一个POI的使用方法。eclipse的插件,操作Excel很强悍,也很简单。
相似回答