poi excel 合并单元格后在插值无效?

penghaozhong 2011-02-17 09:32:16
最近在用struts2导出excel ,用到的插件是poi jar包,现在碰到个问题,在第二次往单元格内插值的时候,第一次插入的值没有了,下面是效果图



...全文
176 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xieshengjun2009 2011-03-03
不会吧,肯定你错了!仔细检查!
回复
penghaozhong 2011-02-19
[Quote=引用 1 楼 woshigaoshou980 的回复:]
给你一个 导出EXCEL 的例子


Java code

package com.zds.emis.employee.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
impo……
[/Quote]
假设一个例子哈 ,不在同一个for 里面赋值,分开赋值,第一次我对姓名排序后填入excel,第一次根据地址排序在填入excel,分开填入excel,在我测试的项目中 ,第二次赋值的情况下,第一次填入excel的值 没见了,搞不明白为什么
回复
上面是 servlet 的
回复
给你一个 导出EXCEL 的例子


package com.zds.emis.employee.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.zds.emis.employee.service.IEmployeeManageBs;
import com.zds.emis.employee.service.imp.EmployeeManageBs;
import com.zds.emis.employee.vo.EmployeeVo;

public class ExportEmployeeExcel extends HttpServlet {

/**
* Constructor of the object.
*/
public ExportEmployeeExcel() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
EmployeeVo employeeVo=new EmployeeVo();
//获取员工编号
employeeVo.setEmpCode(request.getParameter("empCode"));
System.out.println("你要导出的员工编号为:"+request.getParameter("empCode"));
//获取员工姓名
employeeVo.setEmpName(request.getParameter("empName"));

//初始化Service
IEmployeeManageBs service=new EmployeeManageBs();
//掉用Service
List employeeList=service.queryByCondition(employeeVo);
// 用日期构建输出文件名称
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
String date = sd.format(new Date());
String excelName = "员工信息_" + date + ".xls";
// 解决导出员工数据时汉字乱码问题
excelName = new String(excelName.getBytes(), "iso8859-1");
// 选择地址点击保存或者直接点击查看
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=\""+ excelName + "\"");
OutputStream os = response.getOutputStream();
//利用OutputStream创建WritableWorkbook对象
WritableWorkbook wwb = Workbook.createWorkbook(os);
//设置字体 、颜色 、大小
WritableFont font1=new WritableFont(WritableFont.TIMES,11,WritableFont.BOLD,false,
UnderlineStyle.NO_UNDERLINE,Colour.GREEN);
WritableFont font2=new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD);
WritableFont font3=new WritableFont(WritableFont.ARIAL,9,WritableFont.BOLD);
WritableFont font4=new WritableFont(WritableFont.TIMES,11,WritableFont.BOLD,false,
UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat format1=new WritableCellFormat(font1);
WritableCellFormat format2=new WritableCellFormat(font2);
WritableCellFormat format3=new WritableCellFormat(font3);
WritableCellFormat format4=new WritableCellFormat(font4);
try {
//把水平对齐方式指定为居中
format1.setAlignment(jxl.format.Alignment.CENTRE);
format2.setAlignment(jxl.format.Alignment.CENTRE);
format3.setAlignment(jxl.format.Alignment.CENTRE);
format4.setAlignment(jxl.format.Alignment.CENTRE);
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 生成名为"员工信息"的工作表,参数0表示这是第一页
WritableSheet sheet=wwb.createSheet("员工信息",0);
//设置单元表格的宽度为:13
sheet.setColumnView(0,13);
sheet.setColumnView(1,13);
sheet.setColumnView(2,13);
sheet.setColumnView(3,13);
sheet.setColumnView(4,15);
sheet.setColumnView(5,15);
sheet.setColumnView(6,15);
sheet.setColumnView(7,13);
sheet.setColumnView(8,13);
sheet.setColumnView(9,13);
try {
//合并单元表格
sheet.mergeCells(0,0,9,0);
//向表格里插入数据 并设置 format属性
//在Label对象的构造之中指名单元格位置是第一列第一行(0,0)
Label label1 = new Label(0, 0, "员工信息表",format2);
Label label2 = new Label(0, 1, "工号",format4);
Label label3 = new Label(1, 1, "姓名",format1);
Label label4 = new Label(2, 1, "性别",format1);
Label label14 = new Label(3, 1, "年龄",format1);
Label label15 = new Label(4, 1, "电话",format1);
Label label16 = new Label(5, 1, "手机",format1);
Label label17 = new Label(6, 1, "邮件",format1);
Label label18 = new Label(7, 1, "学历",format1);
Label label19 = new Label(8, 1, "地址",format1);
Label label20 = new Label(9, 1, "状态",format1);
// 将定义好的单元格添加到工作表中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label14);
sheet.addCell(label15);
sheet.addCell(label16);
sheet.addCell(label17);
sheet.addCell(label18);
sheet.addCell(label19);
sheet.addCell(label20);
EmployeeVo[] empVo=new EmployeeVo[employeeList.size()];
for (int i = 0; i <employeeList.size(); i++){
empVo[i] =(EmployeeVo) employeeList.get(i);
Label label5 = new Label(0,i+2,""+empVo[i].getEmpCode(),format4);
sheet.addCell(label5);
Label label6=new Label(1,i+2,""+empVo[i].getEmpName(),format3);
sheet.addCell(label6);
Label label7=new Label(2,i+2,"1".equals(empVo[i].getEmpSex())?"男":"女",format3);
sheet.addCell(label7);
jxl.write.Number label8=new jxl.write.Number(3,i+2,empVo[i].getEmpAge(),format3);
sheet.addCell(label8);
Label label9=new Label(4,i+2,""+empVo[i].getEmpPhone(),format3);
sheet.addCell(label9);
Label label10=new Label(5,i+2,""+empVo[i].getEmpMobile(),format3);
sheet.addCell(label10);
Label label11 = new Label(6,i+2,""+empVo[i].getEmpEmail(),format3);
sheet.addCell(label11);
Label label12=new Label(7,i+2,""+empVo[i].getEmpDegree(),format3);
sheet.addCell(label12);
Label label13=new Label(8,i+2,""+empVo[i].getEmpAddress(),format3);
sheet.addCell(label13);
Label label21=new Label(9,i+2,"1".equals(empVo[i].getEmpState())?"在职":"离职",format3);
sheet.addCell(label21);

}
System.out.println("创建成功");
// 写入数据并关闭文件
wwb.write();
wwb.close();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}





}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}

/**
* The doPut method of the servlet. <br>
*
* This method is called when a HTTP put request is received.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Put your code here
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}




回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2011-02-17 09:32
社区公告
暂无公告