前台代码:
第一种:用ajax的方式发送请求,在浏览器如chrome中不出现任何问题,在360或qq浏览器下载的时候,
有一个新建下载任务的弹框,点弹框中的下载或直接打开按钮,会向服务器发送第二次请求,
并且第二次请求中的参数都是null
//下载
function ajaxFileDown() {
var url2="xinziController/exportExcel.do"
var url="xinziController/exportExcelOneA.do"
DownLoad(url, { type: "excel", columns: columns,GZ:GZ,menuId:menuId,charPeriod:charPeriod }, function () {
//alert("导出成功");
});
}
function DownLoad(url, fields, callback) {
//创建Form
var submitfrm = document.createElement("form");
submitfrm.action = url;
submitfrm.method = "post";
submitfrm.target = "_blank";
document.body.appendChild(submitfrm);
if (fields) {
for (var p in fields) {
var input = mini.append(submitfrm, "<input type='hidden' name='" + p + "'>");
var v = fields[p];
if (typeof v != "string") v = mini.encode(v);
input.value = v;
}
}
submitfrm.submit();
setTimeout(function () {
submitfrm.parentNode.removeChild(submitfrm);
if (callback) callback();
}, 1000);
}
第二种:用form表单的方式发送请求,在浏览器如chrome中不出现任何问题,在360或qq浏览器下载的时候,
有一个新建下载任务的弹框,点弹框中的下载或直接打开按钮,会向服务器发送第二次请求,
并且第二次请求中的参数值跟第一次的一样
<form id="empForm" action="xinziController/importExcel<%=type %>.do" method="post" enctype="multipart/form-data">
<input type="file" id="excelPath" name="excelPath" style="width:30%;"/>
<a id="id_dra" class="mini-button" iconCls="icon-upload" οnclick="importE()" plain="true" >导入</a>
<a id="id_dr" class="mini-button" iconCls="icon-download" href="xinziController/exportExcelOne<%=type %>.do" plain="true">下载模板</a>
</form>
后台关键代码:
在//************ 设置单元格内容及格式开始******************和//************ 设置单元格内容及格式开始******************
之间的内容时给表格单元格中增加内容和格式,注释后,不管哪个浏览器都不会出现第二次提交请求,加上后,
在360和qq这种有下载弹框的浏览器中就会发送第二次请求,不知道这是什么问题?
/**
* @param title 表题
* @param headers 表头
* @param dataset 数据
* @param out 输出流
* @param keys HashMap�е�keyֵ
* @param mergedCol 合并列
* @param mergedL
*/
public void exportExcelList(String title, String[] headers,
List<HashMap<Object,Object>> dataset, OutputStream out,String[] keys, int[] mergedCol, int[] mergedL) {
boolean isSH=false;
if(title.startsWith("薪资审核表 ")||title.contains("审核")){
isSH=true;
}
// 创建一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFDataFormat format = workbook.createDataFormat();//����һ��format
//字体
HSSFFont titleFont = workbook.createFont();
titleFont.setFontName("楷体");
titleFont.setColor(HSSFColor.BLUE.index);
HSSFCellStyle titleStyle = createStyleTitle(workbook, titleFont);
titleStyle.setDataFormat(format.getFormat("@"));
//titleStyle.setLocked(true);//锁定单元格
//titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
//表头格式
HSSFFont headerFont = workbook.createFont();
HSSFCellStyle headerStyle = createStyleHeader(workbook, headerFont);
headerStyle.setDataFormat(format.getFormat("@"));
//表体文本数据格式
HSSFFont bodyFont = workbook.createFont();
HSSFCellStyle bodyStyle = createStyleHeader(workbook, bodyFont);
bodyStyle.setDataFormat(format.getFormat("@"));
bodyStyle.setBorderBottom(CellStyle.BORDER_THIN);
bodyStyle.setBorderLeft(CellStyle.BORDER_THIN);
//表体薪资数据格式
HSSFFont normalFont = workbook.createFont();
HSSFCellStyle normalStyle = createStyle(workbook, normalFont);
normalStyle.setDataFormat(format.getFormat("#,#0.00"));
normalStyle.setWrapText(false);
//normalStyle.setLocked(false);//不锁定单元格
HSSFFont longFont = workbook.createFont();
HSSFCellStyle cellLongStyle = createStyle(workbook, longFont);
cellLongStyle.setDataFormat(format.getFormat("0"));
cellLongStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// 创建sheet 名字title
HSSFSheet sheet = workbook.createSheet(title);
//sheet.protectSheet(new String("333"));//333是密码
//创建第一行
HSSFRow row = sheet.createRow(0);
row.setHeightInPoints(24);
sheet.addMergedRegion(new CellRangeAddress(0,0,0,((headers.length -1)/2)));
HSSFCell cell0 = null;
cell0 = row.createCell(0);// 增加单元格
cell0.setCellStyle(titleStyle);
cell0.setCellValue(title);
row.createCell(((headers.length -1)/2)).setCellStyle(titleStyle);//
//创建第二行
row = sheet.createRow(1);
for (int i = 0; i < (headers.length/2); ++i) {
sheet.setColumnWidth(i, 100*35);
HSSFCell cell = row.createCell(i);
cell.setCellStyle(headerStyle);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//创建第三行
row = sheet.createRow(2);
for (int i = 0; i < headers.length/2; ++i) {
sheet.setColumnWidth(i, 100*35);
HSSFCell cell = row.createCell(i);
cell.setCellStyle(headerStyle);
HSSFRichTextString text = new HSSFRichTextString(headers[i+headers.length/2]);
cell.setCellValue(text);
}
boolean b_merge = false;
if((null!=mergedL) && (mergedL.length>0) && (null!=mergedCol) && (mergedCol.length>0)){
b_merge = true;
}
Iterator<HashMap<Object,Object>> it = dataset.iterator();
int index = 2;//表格数据的其实行号
int mCount = 2;
int marrIndex = 0;
String tempCellVal = "";
while (it.hasNext()) {
++index;
if(b_merge){
if(index == mCount){//�ϲ���Ԫ��
if(mergedL[marrIndex]>1){//��Ҫ��ϲ��ĵ�Ԫ����кϲ�
for(int i=0; i<mergedCol.length; ++i){//���κϲ����еĶ��� �����кϲ���
//new Region(��ʼ�У���ʼ�У������У�������)
sheet.addMergedRegion(new CellRangeAddress(mCount, mergedCol[i], mCount+mergedL[marrIndex]-1, mergedCol[i]));
}
mCount += mergedL[marrIndex];
}else{
++mCount;
}
++marrIndex;
}
}
row = sheet.createRow(index);//设置行号
HashMap<Object,Object> t = (HashMap<Object,Object>) it.next();//it代表所有列的列名
//************ 设置单元格内容及格式开始******************
for(int i = 0;i<keys.length;i++){
HSSFCell cell = row.createCell(i);//设定列号
try {
if(t.get(keys[i]).equals(null)||t.get(keys[i])==null){
tempCellVal = "";
}else{
tempCellVal = t.get(keys[i]).toString();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("空指针。。。。。。。。。。。。。");
}
if(isSH){
if (i<2) {//如果有汉字 或 以0 开头的也是文本
cell.setCellValue(tempCellVal);
cell.setCellStyle(bodyStyle);
}else{
cell.setCellValue(Double.valueOf(tempCellVal));//其余的都转换成浮点型
cell.setCellStyle(normalStyle);
}
}else{
if (i<4) {//如果有汉字 或 以0 开头的也是文本
cell.setCellValue(tempCellVal);
cell.setCellStyle(bodyStyle);
}else{
cell.setCellValue(Double.valueOf(tempCellVal));//其余的都转换成浮点型
cell.setCellStyle(normalStyle);
}
}
}
//************ 设置单元格内容及格式结束******************
if(index > 60000){
break;
}
}
if(dataset.size()>0){
//设置最后合计行
row=sheet.createRow(index+1);
StringBuffer sixBuf=new StringBuffer();
for (int i = 0; i < keys.length; i++) {
HSSFCell cell=row.createCell(i);
if(isSH){
if(i==0){
cell.setCellValue("合计:");
sheet.addMergedRegion(new CellRangeAddress(index+1, index+1, 0, 1));
}
if(i<2){
HSSFFont font = workbook.createFont();
cell.setCellStyle(createStyleHJ(workbook, font));
}
if(i>=2){
String chars=getExcelColumn(i+1);
sixBuf.append("SUM("+chars+"4:"+chars+""+(index+1)+")");
cell.setCellFormula(sixBuf.toString());
cell.setCellStyle(normalStyle);
sixBuf.setLength(0);
}
}else{
if(i==0){
cell.setCellValue("合计:");
sheet.addMergedRegion(new CellRangeAddress(index+1, index+1, 0, 3));
}
if(i<4){
HSSFFont font = workbook.createFont();
cell.setCellStyle(createStyleHJ(workbook, font));
}
if(i>=4){
String chars=getExcelColumn(i+1);
sixBuf.append("SUM("+chars+"4:"+chars+""+(index+1)+")");
cell.setCellFormula(sixBuf.toString());
cell.setCellStyle(normalStyle);
sixBuf.setLength(0);
}
}
}
//�Զ�����Ԫ����
for(int i=0; i<headers.length; ++i){
//sheet.autoSizeColumn(i, true);//适合合并单元格
sheet.autoSizeColumn(i);
sheet.setColumnWidth(i, sheet.getColumnWidth(i)+500);
}
}
//导出Excel
try {
workbook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}