本例试用poi 3.0,jdk1.4,weblogic8.13,sqlserver2000
1.excel导入数据
导入时候注意数字类型和字符类型的区别,数字类型要在excel中设置数字类型,对应数据库中也应该建立对应类型
<%@ page import="com.guanri.database.*,java.util.*,com.guanri.util.*,com.jspsmart.upload.*" contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*,java.io.*,org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<jsp:include page="/admin/checkLogin.jsp"/>
<%
String result = "";
String contracId = "";
String projectname = "";
double projectscale = 0;
String projecttype="";
String projectconunit="";
double designtime = 0;
String designphase = "";
double completedtime = 0;
String participants = "";
String positionofpro = "";
String dutiesas = "";
int inum = 0;
int unum = 0;
int rnum = 0;
int snum = 0;
String rmstr = "";
Table tb = new Table();
Vector vc = null;
Hashtable rows = new Hashtable();
String sql = "";
if(request.getMethod().equalsIgnoreCase("POST")) {
String sdir = "/download/xlsdir/"; //文件保存目录
String filesep=System.getProperty("file.separator");
String ssdir = filesep+"download"+filesep+"xlsdir"+filesep;
Calendar c = Calendar.getInstance();
String fname = c.getTimeInMillis()+".xls";
String sfname = sdir + fname;
String ss = "xls"; //支持的文件后缀名
try{
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setAllowedFilesList(ss);
su.upload();
com.jspsmart.upload.File file = su.getFiles().getFile(0);
if(!file.isMissing()){
file.saveAs(sfname);
// System.out.println(application.getRealPath("/")+ssdir+fname );
FileInputStream finput = new FileInputStream(application.getRealPath("/")+ssdir+fname );
//设定FileINputStream读取Excel档
POIFSFileSystem fs = new POIFSFileSystem( finput );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
//读取第一个工作表,宣告其为sheet
finput.close();
HSSFRow row=null;
//宣告一列
HSSFCell cell=null;
//宣告一个储存格
short i=0;
short y=0;
//以巢状迴圈读取所有储存格资料
System.out.println("sheet.getLastRowNum()="+sheet.getLastRowNum());
for (i=1;i<=sheet.getLastRowNum();i++)
{
row=sheet.getRow(i);
for (y=1;y<=row.getLastCellNum();y++)
{
cell=row.getCell(y);
switch(y){
case 1: //合同号
if(cell!=null){
contracId=(cell.getStringCellValue()).trim();
}
break;
case 2: //项目名称
if(cell!=null){
projectname=(cell.getStringCellValue()).trim();
}
break;
case 3: //项目规模
if(cell!=null){
projectscale=cell.getNumericCellValue();
}
break;
case 4: //项目类型
if(cell!=null){
projecttype=(cell.getStringCellValue()).trim();
}
break;
case 5: //项目建设单位
if(cell!=null){
projectconunit=(cell.getStringCellValue()).trim();
}
break;
case 6: //设计时间
if(cell!=null){
designtime=cell.getNumericCellValue();
}
break;
case 7: //设计阶段
if(cell!=null){
designphase=(cell.getStringCellValue()).trim();
}
break;
case 8: //竣工时间
if(cell!=null){
completedtime=cell.getNumericCellValue();
}
break;
case 9: //参与人姓名
if(cell!=null){
participants=(cell.getStringCellValue()).trim();
}
break;
case 10: //担任项目职务
if(cell!=null){
positionofpro=(cell.getStringCellValue()).trim();
}
break;
case 11: //担任职责
if(cell!=null){
dutiesas=(cell.getStringCellValue()).trim();
}
break;
}
}
sql = "insert into hy_pro_briefinfo (contracId,projectname,projectscale,projecttype,projectconunit,designtime,designphase,completedtime,participants,positionofpro,dutiesas) ";
sql+="values ('"+contracId+"','"+projectname+"','"+projectscale+"','"+projecttype+"','"+projectconunit+"','"+(int)designtime+"','"+designphase+"','"+(int)completedtime+"','"+participants+"','"+positionofpro+"','"+dutiesas+"')";
//System.out.println(sql);
snum = tb.update(sql);
if(snum<=0){
++rnum;
rmstr = rmstr + ",";
}else{
++inum;
}
tb.commit();
tb.setCommit(true);
// System.out.println("result="+result);
//System.out.println("snum="+snum +"inum="+inum);
}
result = "共成功上传 "+(inum+unum)+" 条记录,其中新增 "+inum+" 条,更新 "+unum+" 条;失败 "+rnum+" 条( "+rmstr+" )";
// System.out.println(result);
java.io.File f = new java.io.File(application.getRealPath("/")+ssdir+fname);
f.delete();
}else{
result = "上传文件失败";
}
}catch(Exception ex){
ex.printStackTrace();
java.io.File f = new java.io.File(application.getRealPath("/")+ssdir+fname);
//f.delete();
// result = ex.getMessage();
if(result.indexOf("1010")>0){
result = "文件格式不对!只支持"+ss+"格式文件";
}
if("You cannot get a numeric value from a String based cell".equals(result)){
result = "格式错误:在数字单元格中存在非数字!第"+(unum+inum+1)+"条记录出错";
}
}
//finally{
// response.sendRedirect("pro_per_info.jsp");
// }
}
%>
<jsp:include page="/admin/BodyStart" />
Ext.MessageBox.alert("提示",'<%=result %>');
setTimeout("window.location.href='pro_per_info.jsp'", 1000);
<jsp:include page="/admin/BodyEnd" />
2.excel导出数据
导出的时候注意中文编码的问题
response.reset();
response.setContentType("application/vnd.ms-excel");
必须按照先后顺序书写
<%@ page language="java" import="com.guanri.database.*,java.util.*,com.guanri.util.*,com.jspsmart.upload.*" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<%@ page import="java.util.*,java.io.*,org.apache.poi.hssf.usermodel.*" %>
<%
//response.setContentType("application/msexcel;charset=utf-8" );
//response.setContentType("text/html;charset=utf-8");
response.reset();
response.setContentType("application/vnd.ms-excel");
request.setCharacterEncoding("gb2312");
String workSheetName = "pro_info.xls";
response.setHeader("Content-disposition", "attachment; filename="+workSheetName);
//WriteExcel we=new WriteExcel();
//System.out.println("文件生成");
//we.getExcel("steet1",response.getOutputStream());
HSSFWorkbook workbook = new HSSFWorkbook(); //产生工作簿对象
HSSFSheet sheet = workbook.createSheet(); //产生工作表对象
sheet.setDefaultRowHeight((short) 10);
sheet.setDefaultColumnWidth((short) 20);
sheet.setColumnWidth((short) 4, (short) 10000);// 单位
workbook.setSheetName(0,"firstSheet");
// 设置字体
HSSFFont font = workbook.createFont();
// font.setFontHeightInPoints(( short ) 20 ); // 字体高度
// font.setColor(HSSFFont.COLOR_RED); // 字体颜色
font.setFontName( " 黑体 " ); // 字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 宽度
// font.setItalic( true ); // 是否使用斜体
// font.setStrikeout(true); // 是否使用划线
// 设置单元格类型
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 水平布局:居左边
cellStyle.setWrapText( true );
HSSFRow row = sheet.createRow((short)0);//产生第一行
HSSFCell cell = row.createCell((short) 0);//产生第一个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("本公司项目统计信息");
row = sheet.createRow((short) 1);//产生第二行
cell = row.createCell((short) 0);//产生第一个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号");
cell = row.createCell((short) 1);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓名");
cell = row.createCell((short) 2);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("项目名称");
cell = row.createCell((short) 3);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("项目类型");
cell = row.createCell((short) 4);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("项目规模");
cell = row.createCell((short) 5);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("担任项目职务");
cell = row.createCell((short) 6);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("设计时间");
cell = row.createCell((short) 7);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("设计阶段");
cell = row.createCell((short) 8);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("项目建设单位");
cell = row.createCell((short) 9);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("竣工时间");
cell = row.createCell((short) 10);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("专项设计");
cell = row.createCell((short) 11);//产生第二个单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("直属专业领导");
Table tb = new Table();
Vector vc = null;
Hashtable rows=new Hashtable();
String sql = "";
sql = "select * from hy_pro_briefinfo where 1=1";
sql = sql + " order by participants";
vc = tb.getContent(sql);
// System.out.println(sql);
// String result = "";
int order=0;
String contracId = "";
String projectname = "";
String projectscale = "";
String projecttype="";
String projectconunit="";
double designtime = 0;
String designphase = "";
double completedtime = 0;
String participants = "";
String positionofpro = "";
String dutiesas = "";
String leader="";
int iRow=2;
String info="";
String pername="";
int num=1;
for(int i=0;i<vc.size();i++){
rows = (Hashtable)vc.get(i);
order=i+1;
participants=(String)rows.get("participants");
if(!pername.equals(participants)){
pername=participants;
num=1;
}else{
num++;
}
// System.out.println("participants="+participants);
projectname=(String)rows.get("projectname");
projecttype=(String)rows.get("projecttype");
// System.out.println("projectname="+projectname);
positionofpro=(String)rows.get("positionofpro");
// System.out.println("positionofpro="+positionofpro);
designtime=((Integer)rows.get("designtime")).intValue();
projectscale=(Double)rows.get("projectscale")+"";
// System.out.println("designtime="+designtime);
designphase=(String)rows.get("designphase");
// System.out.println("designphase="+designphase);
projectconunit=(String)rows.get("projectconunit");
// System.out.println("projectconunit="+projectconunit);
completedtime=((Integer)rows.get("completedtime")).intValue();
// System.out.println("completedtime="+completedtime);
dutiesas=(String)rows.get("dutiesas");
// System.out.println("dutiesas="+dutiesas);
row= sheet.createRow((short)iRow);;
for(int j=1;j<=11;j++)
{
cell = row.createCell((short)(j-1));
cell.setCellStyle(cellStyle); // 设置单元格样式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch(j-1){
case 0: //编号
info=num+"";
break;
case 1: //姓名
info=participants+"";
break;
case 2: //项目名称
info=projectname+"";
break;
case 3: //项目类型
info=projecttype+"";
break;
case 4: //面积
info="建筑面积:"+projectscale+"平方米";
break;
case 5: //担任项目职务
info=positionofpro+"";
break;
case 6: //设计时间
info=designtime+"";
info=info.substring(0,info.indexOf("."));
break;
case 7: //设计阶段
info=designphase+"";
break;
case 8: //施工单位
info=projectconunit+"";
break;
case 9: //竣工时间
info=completedtime+"";
info=info.substring(0,info.indexOf("."));
if("0".equals(info)){
info="";
}
break;
case 10: //专项设计
info=dutiesas+"";
break; //直属专业领导
case 11:
info=leader+"";
break;
}
//System.out.println("info="+info);
cell.setCellValue(info);
}
iRow++;
}
OutputStream output=response.getOutputStream();
output.flush();
workbook.write(response.getOutputStream());
output.close();
%>