将你的jsp中的定义contentType为:contentType="application/vnd.ms-excel; charset=GBK"就可以了。如果还有疑问:msn:[email protected]
解决方案 »
- 请问要改变视频内的setevent参数,使用什么软件才能做到呢?
- struts+spring+JPA 下的一个常见异常。BeanUtils.populate 急!
- tomcat5.5.7集群怎么操作
- 关于JSP的问题,请大家帮帮忙.
- 一个网站的新闻内容是存再数据库还是文件夹下好?
- 添加多个上传文件名称添加到select列表里?
- StringTemplage的用法?
- 谁可以给我这几个包啊?
- 谁用JSP(NOT JAVASCRIPT)作过树型图,就是资源管理器的那种,请进来谈谈经验或思路.
- spring+quartz 计划任务在0点的时候总会额外触发?
- 急!!!!stuts 页面提交后 转到另一个页面 页面bean没有重新载入,无法显示正确结果
- 一个登陆页面的问题?
然后到你的页面上调用应该可以了!
//定义常量
var xlUnderlineStyleNone = 4294963154 //无下划线
var xlUnderlineStyleSingle = 2 //单下划线
var xlUnderlineStyleDouble = 4294963177 //双下划线var xlGeneral = 1 //常规
var xlLeft = 4294963165 //考左
var xlCenter = 4294963188 //居中
var xlRight = 4294963144 //靠右
var xlFill = 5 //填充
var xlJustify = 1 //两端对齐
var xlCenterAcrossSelection = 7 //跨列居中
var xlDistributed = 4294963179 //分散对齐var xlTop = 4294963136 //靠上
var xlBottom = 4294963189 //靠下var xlDiagonalDown = 5 //下对角线
var xlDiagonalUp = 6 //上对角线
var xlEdgeLeft = 7 //左边线
var xlEdgeTop = 8 //上边线
var xlEdgeBottom = 9 //下边线
var xlEdgeRight = 10 //右边线
var xlInsideVertical = 11 //内竖线
var xlInsideHorizontal = 12 //内横线var xlNone = 4294963154 //无边框的设置值var xlVertical = 4294963130 //字体旋转度数--文本数值排列
var xlAutomatic = 4294963191 //颜色自动var xlContinuous = 1 //联线连续
var xlThin = 2 //连线为窄线var xlLandscape = 2 //横向打印
var xlPortrait = 1 //纵向打印var WM_CLOSE = 16 //WM_CLOSE对应消息IDvar dataArray = new Array();
dataArray[0] =new Array();
dataArray[1] =new Array();
dataArray[2] =new Array();function createExcel()
{
var excelObj;
excelObj = new ActiveXObject( "Excel.Application" );
if( !excelObj ){
alert("无法创建EXCEL!");
}
ActiveWorkbook = excelObj.Application.WorkBooks.Add();
return excelObj;
}function closeExcel(excelObj)
{
excelObj.Application.ActiveWindow.Close();
if( !excelObj.Quit() ){
return true;
}else{
alert("无法关闭EXCEL!");
return false;
}
}function setVisible(excelObj,ab_visible)
{
if( ab_visible ){
excelObj.Application.Visible = true;
}
else{
excelObj.Application.Visible = false;
}
}function setTitle(excelObj,length,titleName)
{
var carry =0,residue=0;
var temp="";
if( length > 26)
{
carry =Math.floor(length/26) ; //返回小于等于其数值参数的最大整数
residue =length%26 ;
temp =String.fromCharCode(64+carry) + String.fromCharCode(64+residue) +1;
}else
{
temp =String.fromCharCode(65+length) +1;
}
combinCells(excelObj,'A1',temp);
setRowHeight(excelObj,1,40);
setAlign(excelObj,'A1',temp,3,2);
setFont(obj,'A1',temp,'隶书','黑体',20);
ls_range = 'A1:' + temp;
excelObj.Range( ls_range ).Select( );
excelObj.ActiveCell.FormulaR1C1 = titleName;
}function saveToFile(excelObj)
{
var myFolder=getFolder();
if(!myFolder)
{
alert("请选择保存路径!");
return;
}
var fileName=getFileName();
ActiveWorkbook.SaveAs( myFolder + fileName +".xls" );
}
function exportToExcel(excelObj)
{
var fileName=getFileName();
var content=getData();
var rowNum=content[0].length;
with(excelObj)
{
Cells.Select;
Selection.NumberFormatLocal = "@" ; for(var i=2;i<rowNum+2;i++)
{
var a=65,b=65;
var firstColSpanContorl=0;
var titlePrinted=0; //控制打印标题次数
var temp1="";
var subContent0 =content[0][i-2].split(",");
var subContent1 =content[1][i-2].split(",");
var subContent2 =content[2][i-2].split(",");
var colNum =subContent0.length; if(i==2 && titlePrinted==0 ) //打印大标题部分
{
var titleLen =colNum;
if(content[0][i-1].split(",").length>colNum)
{
titleLen =content[0][i-1].split(",").length;
}
setTitle(excelObj,titleLen,fileName);
titlePrinted==1;
}
for(var j=0;j<colNum;j++) //打印数据部分
{
var temp=""; var temp2="";
if( a > 90 ){ //列坐标进位控制
a=65;
temp1 =String.fromCharCode(b);
temp = temp1 + String.fromCharCode(a);
b++;
}else{
temp = temp1+ String.fromCharCode(a);
}
var tempVatical=temp;
temp +=i; if( i<3 && subContent2[j] !=1 && subContent2[j].length !=0) //打印小title(第2,3行)
{
if( i==2 && firstColSpanContorl==0 ){
a = a +( Number(subContent2[j])-2 );
firstColSpanContorl=1;
}else{
a = a + Number(subContent2[j])-1;
}
if( a > 90 ){ //进位
a=65;
temp1 =String.fromCharCode(b);
temp2 = temp1 + String.fromCharCode(a);
b++;
}else{
temp2 = temp1+ String.fromCharCode(a);
}
temp2 +=i;
combinCells(excelObj,temp,temp2);
ls_range =temp + ':' + temp2;
Range( ls_range ).Select( );
}else if( i<3 && subContent1[j] !=1 && subContent1[j].length !=0)
{
tempVatical =tempVatical + ( i+ Number(subContent2[j]) );
combinCells(excelObj,temp,tempVatical);
ls_range =temp + ':' + tempVatical;
Range( ls_range ).Select( );
}else{
//Range("A1").Select; 打印数据
Range(temp).Select;
}
ActiveCell.FormulaR1C1 = subContent0[j];
a++;
}
}
}
excelObj.Application.Visible = true;
//ActiveWorkbook.SaveAs( myFolder + fileName +".xls" );
return true;
}function getFolder()
{
var Message = '请选择保存路径';
var Shell = new ActiveXObject( "Shell.Application" );
var Folder = Shell.BrowseForFolder(0,Message,0x0040,0x11);
if(Folder != null)
{
Folder = Folder.items();
Folder = Folder.item();
Folder = Folder.Path;
if(Folder.charAt(Folder.length-1) != "\\")
Folder = Folder + "\\";
return Folder;
}else
{
return false;
}
}function setFolder(excelObj,savePath,fileName)
{
if(savePath.charAt(savePath.length-1) != "\\")
savePath = savePath + "\\";
if( excelObj.ActiveWorkbook.SaveAs( savePath + fileName +".xls" ) )
{
return true;
}else{
alert("文件存储失败,请检查[路径,文件名]是否正确!");
return false;
}
}
{
var fileName="";
var all=document.all;
for(var i=0;i<all.length;i++){
if(all[i].tagName=='TITLE'){
fileName =all[i].text;
break;
}
}
return fileName;
}/*二维数组存储数据:dataArray[0][j]存储数据,
dataArray[1][j]存储rowSpan,
dataArray[2][j]存储colSpan, **********/
function getData()
{
var trFlag=0; //0:不包含tr,1:包含tr
var flag=0; //0:不包含th,1:包含th
var difTable=0; //0:一个表,1:多表
var all=document.all;
var titleLength=0; //存储列的项数
for(var i=0;i<all.length;i++)
{
if(all[i].tagName=='TABLE')
{
row=all[i].rows;
if( difTable ==1 ) //去掉scorllbar占的<td>单元
{
dataArray[1][titleLength+1]=dataArray[1][titleLength+1].substring(0,dataArray[1][titleLength+1].length-1);
dataArray[1][titleLength+2]=dataArray[1][titleLength+2].substring(0,dataArray[1][titleLength+2].length-1);
dataArray[2][titleLength+1]=dataArray[2][titleLength+1].substring(0,dataArray[2][titleLength+1].length-1);
dataArray[2][titleLength+2]=dataArray[2][titleLength+2].substring(0,dataArray[2][titleLength+2].length-1);
dataArray[0][titleLength+1]=dataArray[0][titleLength+1].substring(0,dataArray[0][titleLength+1].length-1);
dataArray[0][titleLength+2]=dataArray[0][titleLength+2].substring(0,dataArray[0][titleLength+2].length-1);
dataArray[0][row.length-1]=dataArray[0][row.length-1].substring(0,dataArray[0][row.length-1].length-1);
}
for(var j=0;j<row.length;j++)
{
if(row[j].tagName=='TR')
{
if( difTable ==0 )
{
for(var k=2;k<row.length;k++) //初始化dataArray[0][j]
{
dataArray[0][k]="";
}
dataArray[1][j]="";
dataArray[2][j]="";
}
cell=row[j].cells;
for(var k=0;k<cell.length;k++)
{
if(cell[k].tagName=='TH' && cell[k].style.display!='none' )
{
titleLength =j;
if(cell[k].rowSpan !=null) {
dataArray[1][j] +=cell[k].rowSpan +","; }
else { dataArray[1][j] += "1,"; }
if(cell[k].colSpan !=null) {
dataArray[2][j] +=cell[k].colSpan +","; }
else { dataArray[2][j] += "1,"; }
child=cell[k].childNodes;
for(var m=0;m<child.length;m++)
{
if(k!=0 || difTable==1)
{
if( cell[k].title != null && cell[k].title.length != 0 )
{
dataArray[0][j] += cell[k].title +",";
}else
{
if(child[m].tagName=='INPUT' || child[m].tagName=='SELECT'){
dataArray[0][j] += child[m].value +",";
}else
{
dataArray[0][j] += cell[k].innerText +",";
}
}
}else
{
dataArray[0][j]=cell[k].innerText+",";
}
}//end for
flag=1;
}
if(cell[k].tagName=='TD' && cell[k].style.display!='none' && flag ==1 )
{
if(cell[k].rowSpan !=null) {
dataArray[1][j] +=cell[k].rowSpan +","; }
else { dataArray[1][j] += "1,"; }
if(cell[k].colSpan !=null) {
dataArray[2][j] +=cell[k].colSpan +","; }
else { dataArray[2][j] += "1,"; }
child=cell[k].childNodes;
if(child.length!=0)
{
for(var m=0;m<child.length;m++)
{
if(k!=0 || difTable==1 )
{
if(cell[k].title != null && cell[k].title.length !=0 )
{
dataArray[0][j] += cell[k].title +",";
}else
{
if(child[m].tagName=='INPUT' || child[m].tagName=='SELECT')
{
dataArray[0][j] += child[m].value +",";
}else
{
if(cell[k].value =='null' && cell[k].innerText.length ==0 )
{
dataArray[0][j] += " ,";
}else{
dataArray[0][j] += cell[k].innerText +",";
}
}
}
}else
{
dataArray[0][j]= (j-titleLength)+",";
}
difTable=1;
} //end for
}else
{
dataArray[0][j] += " ,"; //页面数据为空
}
}
}
}
}//end of second for
}
flag=0;
}//end for
for(var n=0;n<dataArray[0].length;n++) //去掉最后一个","
{
dataArray[0][n]=dataArray[0][n].substring(0,dataArray[0][n].length-1);
}
return dataArray;
}function combinCells(excelObj,as_cellbegin,as_cellend)
{
var ls_cellbegin,ls_cellend;
var ls_range;
ls_cellbegin = as_cellbegin.toUpperCase();
ls_cellend = as_cellend.toUpperCase();
if( !(judgeValidate(ls_cellbegin) && judgeValidate(ls_cellend)) )
{
return;
}
ls_range =ls_cellbegin + ':' + ls_cellend;
excelObj.Range( ls_range ).Select( );
excelObj.Selection.MergeCells = true;
return true;
}function setAlign(excelObj,begin,end,al_halign,al_valign)
{
begin= begin.toUpperCase();
end= end.toUpperCase();
if( !(judgeValidate(begin) && judgeValidate(end)) )
{
return false;
}else{
ls_range = begin + ':' + end;
al_halign= convertHAlign(al_halign);
al_valign= convertVAlign(al_valign);
excelObj.Range( ls_range ).Select();
excelObj.Selection.HorizontalAlignment = al_halign; //3; al_halign ;
excelObj.Selection.VerticalAlignment = al_valign; //2; al_halign;
excelObj.Selection.WrapText = true;
excelObj.Selection.Orientation = 0;
excelObj.Selection.AddIndent = false;
excelObj.Selection.ShrinkToFit = false;
//excelObj.Selection.MergeCells = true;
}
return true;
}//重载setAlign()
/*
function setAlign(excelObj,begin,end,al_halign,al_valign,ab_wraptext,ab_mergecells)
{
begin= begin.toUpperCase();
end= end.toUpperCase();
if( !(judgeValidate(begin) && judgeValidate(end)) )
{
return false;
}else{
ls_range = begin + ':' + end;
excelObj.Range( ls_range ).Select();
al_halign= convertHAlign(al_halign);
al_valign= convertVAlign(al_valign);
excelObj.Selection.HorizontalAlignment = al_halign ;
excelObj.Selection.VerticalAlignment = al_halign ;
excelObj.Selection.WrapText = ab_wraptext ;
excelObj.Selection.MergeCells =ab_mergecells ;
}
return true;
}
*/function setFreeze(excelObj,rowNum,colNum)
{
with(excelObj)
{
excelObj.ActiveWindow.SplitColumn = rowNum ;
excelObj.ActiveWindow.SplitRow = colNum ;
}
excelObj.ActiveWindow.FreezePanes = true ;
}function setRowHeight(excelObj,rowNum,rowHeight) //*****判断行数是否超界*******
{
if( rowNum >65535 || rowNum <0 )
{
return false;
}else{
ls_range = rowNum + ':' + rowNum;
excelObj.Range( ls_range ).Select();
excelObj.Selection.RowHeight = rowHeight ;
}
}
{
//range: A1~IV I:73,v:86,toUpperCase,1~65535
colNum= colNum.toUpperCase();
if( colNum.length >1 && ((colNum.charCodeAt(0)<65 || colNum.charCodeAt(0)>73) || (colNum.charCodeAt(1)<65 || colNum.charCodeAt(0)>86)) )
{
return false;
}else if( colNum.length =1 && (colNum.charCodeAt(0)<65 || colNum.charCodeAt(0)>90) )
{
return false;
}else if( colNum.length =0 ){
return false;
}else{
colNum =colNum.substring(0,3);
ls_range = colNum + ':' + colNum;
excelObj.Range( ls_range ).Select();
excelObj.Selection.ColumnWidth = colWidth ;
}
}function autoFit(excelObj,begin,end,ab_width,ab_height)
{
begin= begin.toUpperCase();
end= end.toUpperCase();
if( !(judgeValidate(begin) && judgeValidate(end)) )
{
return false;
}else{
ls_range = begin + ':' + end;
excelObj.Range( ls_range ).Select();
//设置行宽
if( ab_width = true ){
excelObj.Selection.Columns.AutoFit ;
}
//设置行高
if( ab_height = true ){
excelObj.Selection.Rows.AutoFit ;
}
}
}function setFont(excelObj,begin,end,as_fontname,as_fontstyle,ai_fontsize)
{
begin= begin.toUpperCase();
end= end.toUpperCase();
if( !(judgeValidate(begin) && judgeValidate(end)) )
{
return false;
}else{
ls_range = begin + ':' + end;
excelObj.Range( ls_range ).Select();
excelObj.Selection.Font.Name = as_fontname ;
excelObj.Selection.Font.FontStyle = as_fontstyle ;
excelObj.Selection.Font.Size = ai_fontsize ;
}
return true;
}function setFrame(excelObj,begin,end,ai_top,ai_bottom,ia_left,ai_right,ai_vertical,ai_horizon)
{
begin= begin.toUpperCase();
end= end.toUpperCase();
if( !(judgeValidate(begin) && judgeValidate(end)) )
{
return false;
}else{
ls_range = begin + ':' + end;
excelObj.Range( ls_range ).Select();
//设置两个斜边框
//excelObj.Selection.Borders(xlDiagonalDown).LineStyle = xlNone ;
//excelObj.Selection.Borders(xlDiagonalUp).LineStyle = xlNone ; //上边框
if(ai_top == 1){
excelObj.Selection.Borders(xlEdgeTop).LineStyle = xlContinuous ;
excelObj.Selection.Borders(xlEdgeTop).Weight = xlThin ;
//excelObj.Selection.Borders(xlEdgeTop).ColorIndex = xlAutomatic ;
}else{
excelObj.Selection.Borders(xlEdgeTop).LineStyle = xlNone ;
}
//下边框
if(ai_bottom == 1){
excelObj.Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous ;
excelObj.Selection.Borders(xlEdgeBottom).Weight = xlThin ;
//excelObj.Selection.Borders(xlEdgeBottom).ColorIndex = xlAutomatic ;
}else{
excelObj.Selection.Borders(xlEdgeBottom).LineStyle = xlNone ;
}
//左边框
if(ia_left == 1){
excelObj.Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous ;
excelObj.Selection.Borders(xlEdgeLeft).Weight = xlThin ;
//excelObj.Selection.Borders(xlEdgeLeft).ColorIndex = xlAutomatic ;
}else{
excelObj.Selection.Borders(xlEdgeLeft).LineStyle = xlNone ;
}
//右边框
if(ai_right == 1){
excelObj.Selection.Borders(xlEdgeRight).LineStyle = xlContinuous ;
excelObj.Selection.Borders(xlEdgeRight).Weight = xlThin ;
//excelObj.Selection.Borders(xlEdgeRight).ColorIndex = xlAutomatic ;
}else{
excelObj.Selection.Borders(xlEdgeRight).LineStyle = xlNone ;
}
//内竖边框
if(ai_vertical == 1){
excelObj.Selection.Borders(xlInsideVertical).LineStyle = xlContinuous ;
excelObj.Selection.Borders(xlInsideVertical).Weight = xlThin ;
//excelObj.Selection.Borders(xlInsideVertical).ColorIndex = xlAutomatic ;
}else{
excelObj.Selection.Borders(xlInsideVertical).LineStyle = xlNone ;
}
//内横边框
if(ai_horizon == 1){
excelObj.Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous ;
excelObj.Selection.Borders(xlInsideHorizontal).Weight = xlThin ;
//excelObj.Selection.Borders(xlInsideHorizontal).ColorIndex = xlAutomatic ;
}else{
excelObj.Selection.Borders(xlInsideHorizontal).LineStyle = xlNone ;
}
}
return true;
}function fillCell(excelObj,begin,end,as_content)
{
begin= begin.toUpperCase();
end= end.toUpperCase();
if( !(judgeValidate(begin) && judgeValidate(end)) )
{
return false;
}else{
//如果指定行列不是一个单元格,则首先合并
if( begin != end ){
//合并指定行列
combinCells( excelObj,begin,end );
}
ls_range = begin + ':' + end ;
//开始填充数据
excelObj.Range( ls_range ).Select( );
excelObj.ActiveCell.FormulaR1C1 = as_content ;
}
}function fillCell(excelObj,begin,end,as_content,al_halign,al_valign)
{
begin= begin.toUpperCase();
end= end.toUpperCase();
if( !(judgeValidate(begin) && judgeValidate(end)) )
{
return false;
}else{
//如果指定行列不是一个单元格,则首先合并
if( begin != end ){
//合并指定行列
combinCells( excelObj,begin,end );
}
ls_range = begin + ':' + end ;
//开始填充数据
excelObj.Range( ls_range ).Select( );
//setAlign(excelObj,begin,end,al_halign,al_valign);
excelObj.ActiveCell.FormulaR1C1 = as_content ;
}
}
///////////////////////////////////////////////////////////////////range: (A1~IV65536) A~IV(A:65,I:73,v:86);1~65535
function judgeValidate( begend ) //*****需要判断行数是否超界*******
{
var para_invalidate=0;
begend = begend.toUpperCase();
var begend_0= begend.charCodeAt(0);
var begend_1= begend.charCodeAt(1);
if( begend.length >2 ) //三个或以上字符
{
if(begend_0>73 && begend_0<91) // 第一个 j~z
{
para_invalidate =borderJudge(begend,1); //第二个以后判断是否为数字
}else if(begend_0==73 && (begend_1>64 && begend_1<87)) // 第一个 i
{
para_invalidate =borderJudge(begend,2); //第三个以后判断是否为数字
}else if(begend_0>64 && begend_0<73) // 第一个 a~h
{
if(begend_1>64 && begend_1<91) // a<第二个字 <z
{
para_invalidate =borderJudge(begend,2);
}else if(begend_1>47 && begend_1<58) //第二个字是数字:48~57
{
para_invalidate =borderJudge(begend,1);
}else{
para_invalidate =1;
}
}else{
para_invalidate=1;
}
}else if( begend.length ==2 && begend_0>64 && begend_0<96 && begend_1>47 && begend_1<58 )
{
para_invalidate=0;
}else{
para_invalidate=1;
}
if(para_invalidate ==1)
{
alert("请输入正确的行列参数!");
return false;
}else
{
return true;
}
} function borderJudge(begend,start)
{
var para_invalidate=0;
for(var i=start;i<begend.length;i++)
{
if( begend.charCodeAt(i)<48 || begend.charCodeAt(i)>57 )
{
para_invalidate=1;
break;
}else{
para_invalidate=0;
}
}
if( para_invalidate==0 && (parseInt(begend.substring(start,begend.length)))>65536 )
{ //是数字的情况下判断是否越界 >65536
para_invalidate=1;
}
return para_invalidate;
}function convertHAlign(al_halign)
{
switch( al_halign )
{
case 1: //常规,xlGeneral
al_halign = 1 ;
break;
case 2: //靠左,xlLeft
al_halign = 4294963165;
break;
case 3: //居中,xlCenter:4294963188
al_halign = 3;
break;
case 4: //靠右,xlRight
al_halign = 4294963144;
break;
case 5: //填充,xlFill
al_halign = 5 ;
break;
case 6: //两端对齐,xlJustify ********** 4294963166 ? 1
al_halign = 4294963166;
break;
case 7: //跨列居中,xlCenterAcrossSelection
al_halign = 7;
break;
case 8: //分散对齐,xlDistributed
al_halign = 4294963179;
break;
}
return al_halign;
}function convertVAlign(al_valign)
{
switch( al_valign )
{
case 1: //靠上,xlTop
al_valign = 4294963136 ;
break;
case 2: //居中,xlCenter:4294963188
al_valign = 2;
break;
case 3: //靠下,xlBottom
al_valign = 4294963189 ;
break;
case 4: //两端对齐,xlJustify
al_valign = 1 ;
break;
case 5: //分散对齐,xlDistributed
al_valign = 4294963179 ;
break;
}
return al_valign;
}
但是如果页面里有JSP的代码,
那么就好像不行
如果我的TABLE里的数据是用JSP获得的,好像就不行
如果是静态的我手动写就可以