日期:2014-05-18  浏览次数:20607 次

一个jsp导出excel的问题
我用的是jxl,想在服务器上先建一个excel模板,然后用户在客户端输入信息后,点击生成数据按钮后,服务器往excel模板里写数据,数据是jsp页面采集的由用户输入的信息,循环的往excel模板写入数据,最后生成新的excel表格!我是新手啊,希望大家帮帮我啊~~谢谢各位了啊~~

------解决方案--------------------
//Create Excle workbook
public boolean writeToSheet(int id) throws SQLException,IOException
{
//Write to sheet
try{
if (this.copyFile(pathFrom,pathTo))
{
String filename = pathTo;
short rownumber = 1;
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename + "/Student.xls "));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
ArrayList arrstu = (ArrayList)Student.getAllStudents(id);

//Write to sheet
for(int j=0; j < arrstu.size();j++)
{
Student stu = (Student)arrstu.get(j);
//Get Row
HSSFRow row = sheet.getRow(rownumber);
if (row == null)
{
row =sheet.createRow(rownumber);
}
//Get Name cell
HSSFCell cellname = row.getCell((short)0);
if(cellname == null)
{
cellname = row.createCell((short)0);
}
cellname.setCellType(HSSFCell.CELL_TYPE_STRING);
cellname.setCellValue(stu.getStuname());

//Get Number cell
HSSFCell cellnum = row.getCell((short)1);
if(cellnum == null)
{
cellnum = row.createCell((short)1);
}
cellnum.setCellType(HSSFCell.CELL_TYPE_STRING);
cellnum.setCellValue(stu.getStunum());

//Get Age Cell
HSSFCell cellage = row.getCell((short)2);
if(cellage == null)
{
cellage = row.createCell((short)2);
}
cellage.setCellType(HSSFCell.CELL_TYPE_STRING);
cellage.setCellValue(String.valueOf(stu.getStuage()));

//Get Sex Cell
HSSFCell cellsex = (HSSFCell)row.getCell((short)3);
if(cellsex == null)
{
cellsex = row.createCell((short)3);
}
cellsex.setCellType(HSSFCell.CELL_TYPE_STRING);
//Male
if(stu.getStusex().equals( "1 "))
{
cellsex.setCellValue( "MALE ");
}
else
{
cellsex.setCellValue( "FAMALE ");
}

//Get Information Cell
HSSFCell cellinfo = row.getCell((short)4);
if(cellinfo == null)
{
cellinfo = row.createCell((short)4);
}
cellinfo.setCellType(HSSFCell.CELL_TYPE_STRING);
cellinfo.setCellValue(stu.getStuinfo());

rownumber++;
}
// Write the