这个需要jxl包。
try{
OutputStream os = response.getOutputStream();
WritableFont wf = new WritableFont(WritableFont.MS,12,WritableFont.NO_BOLD,false);
WritableCellFormat wcfF = new WritableCellFormat(wf);
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("Test Sheet 1", 0); Label labelC = new Label(0,0,"姓名",wcfF);
ws.addCell(labelC); wwb.write();
wwb.close();
os.close();
}
catch(Exception e){
e.printStackTrace();
}
try{
OutputStream os = response.getOutputStream();
WritableFont wf = new WritableFont(WritableFont.MS,12,WritableFont.NO_BOLD,false);
WritableCellFormat wcfF = new WritableCellFormat(wf);
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("Test Sheet 1", 0); Label labelC = new Label(0,0,"姓名",wcfF);
ws.addCell(labelC); wwb.write();
wwb.close();
os.close();
}
catch(Exception e){
e.printStackTrace();
}
// 把数据读入到excel中
for (int i = 1; i <= dataList.size(); i++) {
khInformation = (KhInformation) dataList.get(i - 1);
sheet.addCell(new Label(0, i, khInformation.getId() + ""));
sheet.addCell(new Label(1, i, khInformation
.getInformationyear()));
sheet.addCell(new Label(2, i,
khInformation.getPostno() == null ? "" : khInformation
.getPostno()));
sheet.addCell(new Label(3, i,
khInformation.getPostaddress() == null ? ""
: khInformation.getPostaddress()));
sheet.addCell(new Label(4, i,
khInformation.getGetaddress() == null ? ""
: khInformation.getGetaddress()));
sheet.addCell(new Label(5, i,
khInformation.getPostname() == null ? ""
: khInformation.getPostname()));
sheet.addCell(new Label(6, i,
khInformation.getXingbie() == null ? "" : khInformation
.getXingbie()));
sheet.addCell(new Label(7, i,
khInformation.getTelephone() == null ? ""
: khInformation.getTelephone()));
sheet.addCell(new Label(8, i,
khInformation.getGetpostname() == null ? ""
: khInformation.getGetpostname()));
sheet.addCell(new Label(9, i, khInformation
.getGetposttelephone() == null ? "" : khInformation
.getGetposttelephone()));
sheet.addCell(new Label(10, i,
khInformation.getGetalladdress() == null ? ""
: khInformation.getGetalladdress()));
sheet.addCell(new Label(11, i,
khInformation.getZhongliang() == null ? ""
: khInformation.getZhongliang()));
sheet.addCell(new Label(12, i,
khInformation.getPostclass() == null ? ""
: khInformation.getPostclass()));
sheet.addCell(new Label(13, i,
khInformation.getTelclass() == null ? ""
: khInformation.getTelclass()));
sheet.addCell(new Label(14, i,
khInformation.getChaxun() == null ? "" : khInformation
.getChaxun()));
sheet.addCell(new Label(15, i,
khInformation.getChuli() == null ? "" : khInformation
.getChuli()));
sheet.addCell(new Label(16, i,
khInformation.getCompanyname() == null ? ""
: khInformation.getCompanyname()));
sheet.addCell(new Label(17, i, khInformation
.getCompanytelephone() == null ? "" : khInformation
.getCompanytelephone()));
sheet.addCell(new Label(18, i, khInformation
.getCompanyusername() == null ? "" : khInformation
.getCompanyusername()));
sheet.addCell(new Label(19, i,
khInformation.getTaidu() == null ? "" : khInformation
.getTaidu()));
sheet.addCell(new Label(20, i,
khInformation.getNeirong() == null ? "" : khInformation
.getNeirong()));
sheet.addCell(new Label(21, i,
khInformation.getBz() == null ? "" : khInformation
.getBz()));
sheet.addCell(new Label(22, i,
khInformation.getJieguo() == null ? "" : khInformation
.getJieguo()));
sheet.addCell(new Label(23, i,
khInformation.getBz1() == null ? "" : khInformation
.getBz1()));
sheet.addCell(new Label(24, i,
khInformation.getCreateUserNo() == null ? ""
: khInformation.getCreateUserNo()));
if (khInformation.getCreateDateTime() == null) {
sheet.addCell(new Label(25, i, ""));
} else {
sheet.addCell(new Label(25, i, formatter
.format(khInformation.getCreateDateTime())));
}
sheet
.addCell(new Label(26, i, khInformation
.getUpdateUserNo()));
if (khInformation.getUpdateDateTime() == null) {
sheet.addCell(new Label(27, i, ""));
} else {
sheet.addCell(new Label(27, i, formatter
.format(khInformation.getUpdateDateTime())));
}
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var PrintA = document.getElementById("table id");
var sel=document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
按钮调用这个方法
页面里写一个<TABLE>就是你要导入的数据,然后调用上面的方法web.xml下加:
<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>
需要导出的文件头上加:
<%@ page contentType="application/vnd.ms-excel" %>