最近经常有用到导出excel的操作 ,所以就记录一下
用到的jar包是 jxl.jar 点击下载--jxl.jar
准备好这些之后 就是 查看代码了 例子:
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.CellFormat;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* 根据时间导出excel模板信息
* @param request
* @param map
* @return
*/
@SuppressWarnings("deprecation")
@RequestMapping("doExcelTemplate")
@ResponseBody
public ModelMap doExcelTemplate(HttpServletRequest request, ModelMap map) {
try
{
File f=new File(request.getRealPath("/")+"excel/模板信息详情导出.xls");
if(!f.exists())
{
f.createNewFile();
}
WritableWorkbook workbook = Workbook.createWorkbook(f);
WritableSheet sheet = workbook.createSheet("模板信息详情导出",0);
WritableFont wfHeader=new WritableFont(WritableFont.TIMES,14,WritableFont.BOLD);
WritableCellFormat wcHeader=new WritableCellFormat(wfHeader);
wcHeader.setAlignment(Alignment.CENTRE);
wcHeader.setVerticalAlignment(VerticalAlignment.CENTRE);
WritableFont wfCell=new WritableFont(WritableFont.TIMES,10,WritableFont.NO_BOLD);
WritableCellFormat wcCell=new WritableCellFormat(wfCell);
wcCell.setAlignment(Alignment.CENTRE);
wcCell.setVerticalAlignment(VerticalAlignment.CENTRE);
sheet.setRowView(0, 600);
sheet.setColumnView(0, 10);
sheet.setColumnView(1, 30);
sheet.setColumnView(2, 30);
Label lb;
Number nb;
lb=new Label(0,0,"id",wcHeader);
sheet.addCell(lb);
lb=new Label(1,0,"日期",wcHeader);
sheet.addCell(lb);
lb=new Label(2,0,"月份",wcHeader);
sheet.addCell(lb);
lb=new Label(3,0,"主题",wcHeader);
sheet.addCell(lb);
lb=new Label(4,0,"总人数",wcHeader);
sheet.addCell(lb);
lb=new Label(5,0,"PV",wcHeader);
sheet.addCell(lb);
lb=new Label(6,0,"UV",wcHeader);
sheet.addCell(lb);
lb=new Label(7,0,"打开率",wcHeader);
sheet.addCell(lb);
lb=new Label(8,0,"平均打开率",wcHeader);
sheet.addCell(lb);
String time = request.getParameter("time");
List list = timingSendService.timingExecls(time);//查数据库数据
int i=1;
float count=0;
for(WeixinTimingSend obj:list)
{
float PV = 10;//查询数据库
float UV = 20;//查询数据库
String times = obj.getSend_time().toString();
String sendtime=times.substring(0,times.length() - 2);//发送时间
String group="2";//月份分组
String name = obj.getName();//主题
float send_numbe=obj.getSend_numbe();//发送总人数
float sum_uv=UV/send_numbe;
DecimalFormat fnum=new DecimalFormat("##0.00");
String sum=fnum.format(sum_uv); //打开率
count=count+Float.parseFloat(sum);
nb=new Number(0,i,i,wcCell);
sheet.addCell(nb);
lb=new Label(1,i,sendtime,wcCell);//发送时间
sheet.addCell(lb);
lb=new Label(2,i,group,wcCell);//月份
sheet.addCell(lb);
lb=new Label(3,i,name,wcCell);//主题
sheet.addCell(lb);
lb=new Label(4,i,send_numbe+"",wcCell);//发送总人数
sheet.addCell(lb);
lb=new Label(5,i,PV+"",wcCell);
sheet.addCell(lb);
lb=new Label(6,i,UV+"",wcCell);
sheet.addCell(lb);
lb=new Label(7,i,sum,wcCell);//打开率
sheet.addCell(lb);
lb=new Label(8,i,"0",wcCell);//平均打开率
sheet.addCell(lb);
i++;
}
float count_sum=count/list.size();
DecimalFormat fnum = new DecimalFormat("##0.00");
String countSum=fnum.format(count_sum); //平均打开率
//修改
WritableCell cell =sheet.getWritableCell(8, 1);//获取第一个单元格
CellFormat cf = cell.getCellFormat();//获取第一个单元格的格式
Label lbl = new Label(8, 1,countSum+"");//将第一个单元格的值改为“修改後的值”
lbl.setCellFormat(cf);//将修改后的单元格的格式设定成跟原来一样
sheet.addCell(lbl);
//合并单元格,第一个参数:要合并的单元格最左上角的列号,//第二个参数:要合并的单元格最左上角的行号,
//第三个参数:要合并的单元格最右角的列号,//第四个参数:要合并的单元格最右下角的行
sheet.mergeCells(8, 1, 8, i-1);
sheet.mergeCells(1, 1, 1, i-1);
workbook.write();
workbook.close();
map.put("excelurl",request.getContextPath()+"/excel/模板信息详情导出.xls");
}
catch(Exception e)
{
logger.error("AdminRoleAction@_query error", e);
sendFailureMessage(map, "系统内部错误.");
}
return map;
}
然后我们再说说 jxl的修改和合并 : 因为公司需求是要合并平均数,然后excel 合并 如果内容不一样只取第一个内容的值进行合并,所以我这里就是把第一个内容修改为自己最后算出的平均值,这样就完美解决了合并的问题。
float count_sum=count/list.size();
DecimalFormat fnum = new DecimalFormat("##0.00");
String countSum=fnum.format(count_sum); //平均打开率
//jxl 修改
WritableCell cell =sheet.getWritableCell(8, 1);//获取需要修改的第几列 和第几行
CellFormat cf = cell.getCellFormat();//获取第一个单元格的格式
Label lbl = new Label(8, 1,countSum+"");//将第一个单元格的值改为最新的值 我这里是 countSum 平均值
lbl.setCellFormat(cf);//将修改后的单元格的格式设定成跟原来一样
sheet.addCell(lbl);//重新保存
//jxl 合并
//合并单元格,第一个参数:要合并的单元格最左上角的列号,//第二个参数:要合并的单元格最左上角的行号,
//第三个参数:要合并的单元格最右角的列号,//第四个参数:要合并的单元格最右下角的行
sheet.mergeCells(1, 1, 1, i-1); //这里的意思是 第一列的第一行 到第一列的N行 进行合并 (i根据自己的数据库数字进行改变)
sheet.mergeCells(8, 1, 8, i-1); //这里的意思是 第八列的第一行 到第八列的N行 进行合并 (i根据自己的数据库数字进行改变)
最后在附上项目上实际运用,与数据库进行交互的代码:
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.CellFormat;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* 根据时间导出excel模板信息
* @param request
* @param map
* @return
*/
@SuppressWarnings("deprecation")
@RequestMapping("doExcelTemplate")
@ResponseBody
public ModelMap doExcelTemplate(HttpServletRequest request, ModelMap map) {
try
{
File f=new File(request.getRealPath("/")+"excel/模板信息详情导出.xls");
if(!f.exists())
{
f.createNewFile();
}
WritableWorkbook workbook = Workbook.createWorkbook(f);
WritableSheet sheet = workbook.createSheet("模板信息详情导出",0);
WritableFont wfHeader=new WritableFont(WritableFont.TIMES,14,WritableFont.BOLD);
WritableCellFormat wcHeader=new WritableCellFormat(wfHeader);
wcHeader.setAlignment(Alignment.CENTRE);
wcHeader.setVerticalAlignment(VerticalAlignment.CENTRE);
WritableFont wfCell=new WritableFont(WritableFont.TIMES,10,WritableFont.NO_BOLD);
WritableCellFormat wcCell=new WritableCellFormat(wfCell);
wcCell.setAlignment(Alignment.CENTRE);
wcCell.setVerticalAlignment(VerticalAlignment.CENTRE);
sheet.setRowView(0, 600);
sheet.setColumnView(0, 10);
sheet.setColumnView(1, 30);
sheet.setColumnView(2, 30);
Label lb;
Number nb;
lb=new Label(0,0,"id",wcHeader);
sheet.addCell(lb);
lb=new Label(1,0,"日期",wcHeader);
sheet.addCell(lb);
lb=new Label(2,0,"月份",wcHeader);
sheet.addCell(lb);
lb=new Label(3,0,"主题",wcHeader);
sheet.addCell(lb);
lb=new Label(4,0,"总人数",wcHeader);
sheet.addCell(lb);
lb=new Label(5,0,"PV",wcHeader);
sheet.addCell(lb);
lb=new Label(6,0,"UV",wcHeader);
sheet.addCell(lb);
lb=new Label(7,0,"打开率",wcHeader);
sheet.addCell(lb);
lb=new Label(8,0,"平均打开率",wcHeader);
sheet.addCell(lb);
String time = request.getParameter("time");
List list = timingSendService.timingExecls(time);
int i=1;
float count=0;
for(WeixinTimingSend obj:list)
{
DxwGroup adminDxwGroup = dxwGroupService.findDxwGroupById(obj.getSend_group_id());
WeixinTemplatePreset templatePreset = templatePresetService.findPresetPage(Integer.parseInt(obj.getSend_content_id()));
long sqlLastTime = obj.getSend_time().getTime() / 1000;// 直接转换成long
WeixinArticleStatistics articleStatistics = new WeixinArticleStatistics();
articleStatistics.setTiming_time(String.valueOf(sqlLastTime));
articleStatistics.setType(9);
articleStatistics.setArticle_id(obj.getArticle_id());
float PV = templatePresetService.findArtStatisticsPV(articleStatistics);
float UV = templatePresetService.findArtStatisticsUV(articleStatistics);
String times = obj.getSend_time().toString();
String sendtime=times.substring(0,times.length() - 2);//发送时间
String group=null;//月份分组
if(adminDxwGroup!=null)
{
group=adminDxwGroup.getName();
}
String name = templatePreset.getName();//主题
float send_numbe=obj.getSend_numbe();//发送总人数
float sum_uv=UV/send_numbe;
DecimalFormat fnum = new DecimalFormat("##0.00");
String sum=fnum.format(sum_uv); //打开率
count=count+Float.parseFloat(sum);
nb=new Number(0,i,i,wcCell);
sheet.addCell(nb);
lb=new Label(1,i,sendtime,wcCell);//发送时间
sheet.addCell(lb);
lb=new Label(2,i,group,wcCell);//月份
sheet.addCell(lb);
lb=new Label(3,i,name,wcCell);//主题
sheet.addCell(lb);
lb=new Label(4,i,send_numbe+"",wcCell);//发送总人数
sheet.addCell(lb);
lb=new Label(5,i,PV+"",wcCell);
sheet.addCell(lb);
lb=new Label(6,i,UV+"",wcCell);
sheet.addCell(lb);
lb=new Label(7,i,sum,wcCell);//打开率
sheet.addCell(lb);
lb=new Label(8,i,"0",wcCell);//平均打开率
sheet.addCell(lb);
i++;
}
float count_sum=count/list.size();
DecimalFormat fnum = new DecimalFormat("##0.00");
String countSum=fnum.format(count_sum); //平均打开率
//修改
WritableCell cell =sheet.getWritableCell(8, 1);//获取第一个单元格
CellFormat cf = cell.getCellFormat();//获取第一个单元格的格式
Label lbl = new Label(8, 1,countSum+"");//将第一个单元格的值改为“修改後的值”
lbl.setCellFormat(cf);//将修改后的单元格的格式设定成跟原来一样
sheet.addCell(lbl);
//合并单元格,第一个参数:要合并的单元格最左上角的列号,//第二个参数:要合并的单元格最左上角的行号,
//第三个参数:要合并的单元格最右角的列号,//第四个参数:要合并的单元格最右下角的行
sheet.mergeCells(8, 1, 8, i-1);
sheet.mergeCells(1, 1, 1, i-1);
workbook.write();
workbook.close();
map.put("excelurl",request.getContextPath()+"/excel/模板信息详情导出.xls");
}
catch(Exception e)
{
logger.error("AdminRoleAction@_query error", e);
sendFailureMessage(map, "系统内部错误.");
}
return map;
}
结果如下:
如果 还有什么疑问 可以看我的主页 入群一起交流;互相进步 群qq 93472007