利用客户端导出为excel的方法对客户端有很多的要求,不推荐使用
转自:
http://bavnnsym.javaeye.com/blog/259963
http://zhidao.baidu.com/question/104369456.html
使用JavaScript中的ActiveXObject填充并设置Excel格式
1创建
var XLObj = new ActiveXObject("Excel.Application" );
var xlBook = XLObj.Workbooks.Add; //新增工作簿
var ExcelSheet = xlBook.Worksheets(1); //创建工作表
2.保存表格
ExcelSheet.SaveAs("C://TEST.XLS" );
3.使 Excel 通过 Application 对象可见
ExcelSheet.Application.Visible = true;
4.打印
xlBook.PrintOut;
或者:
ExcelSheet.PrintOut;
5.关闭
xlBook.Close(savechanges=false);
或者:
ExcelSheet.Close(savechanges=false);
6.结束进程
ExcelSheet.Application.Quit();
或者:
XLObj.Quit();
XLObj=null;
7.页面设置
ExcelSheet.ActiveSheet.PageSetup.LeftMargin= 2/0.035;
//页边距 左2厘米
ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035;
//页边距右3厘米
ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035;
//页边距上4厘米
ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035;
//页边距下5厘米
ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035;
//页边距页眉1厘米
ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035;
//页边距页脚2厘米
ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "页眉中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "页眉左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightHeader = "页眉右部内容";
ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "页脚中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "页脚左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightFooter = "页脚右部内容";
8.对单元格操作,带*部分对于行,列,区域都有相应属性
ExcelSheet.ActiveSheet.Cells(row,col).Value = "内容";
//设置单元格内容
ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1;
//设置单元格边框*()
ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1;
//设置单元格底色*(1-黑色,2-白色,3-红色,4-绿色,5-蓝色,6-黄色,7-粉红色,8-天蓝色,9-酱土色..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1;
//设置单元格背景样式*(1-无,2-细网格,3-粗网格,4-斑点,5-横线,6-竖线..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1;
//设置字体颜色*(与上相同)
ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10;
//设置为10号字*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = "黑体";
//设置为黑体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Italic = true;
//设置为斜体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Bold = true;
//设置为粗体*
ExcelSheet.ActiveSheet.Cells(row,col).ClearContents;
//清除内容*
ExcelSheet.ActiveSheet.Cells(row,col).WrapText=true;
//设置为自动换行*
ExcelSheet.ActiveSheet.Cells(row,col).HorizontalAlignment = 3;
//水平对齐方式枚举* (1-常规,2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)
ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2;
//垂直对齐方式枚举*(1-靠上,2-居中,3-靠下,4-两端对齐,5-分散对齐)
行,列有相应操作:
ExcelSheet.ActiveSheet.Rows(row).
ExcelSheet.ActiveSheet.Columns(col).
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).
//如Rows("1:5" )即1到5行
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).
//如Columns("1:5" )即1到5列
区域有相应操作:
XLObj.Range(startcell+":"+endcell).Select;
//如Range("A2:H8" )即A列第2格至H列第8格的整个区域
XLObj.Selection.
合并单元格
XLObj.Range(startcell+":"+endcell).MergeCells = true;
//如Range("A2:H8" )即将A列第2格至H列第8格的整个区域合并为一个单元格
XLObj.Range("A2",XLObj.Cells(8,8)).MergeCells = true;
9.设置行高与列宽
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;
//设置从firstcol到stopcol列的宽度为22
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight = 22;
//设置从firstrow到stoprow行的宽度为22
Automation server can't create object , AUTOMATION服务器无法创建对象
方法一:
开始-运行:regsvr32 scrrun.dll
方法二:
请将以下语句复制到记事本中,另存为后缀为.cmd的文件,并运行。当然在命令行下一句句运行也没问题。
echo 正在修复,这个过程可能需要几分钟,请稍候……
rundll32.exe advpack.dll /DelNodeRunDLL32 %systemroot%/System32/dacui.dll
rundll32.exe advpack.dll /DelNodeRunDLL32 %systemroot%/Catroot/icatalog.mdb
regsvr32 /s comcat.dll
regsvr32 /s asctrls.ocx
regsvr32 /s oleaut32.dll
regsvr32 /s shdocvw.dll /I
regsvr32 /s shdocvw.dll
regsvr32 /s browseui.dll
regsvr32 /s browseui.dll /I
regsvr32 /s msrating.dll
regsvr32 /s mlang.dll
regsvr32 /s hlink.dll
regsvr32 /s mshtml.dll
regsvr32 /s mshtmled.dll
regsvr32 /s urlmon.dll
regsvr32 /s plugin.ocx
regsvr32 /s sendmail.dll
regsvr32 /s mshtml.dll /i
regsvr32 /s scrobj.dll
regsvr32 /s corpol.dll
regsvr32 /s jscript.dll
regsvr32 /s msxml.dll
regsvr32 /s imgutil.dll
regsvr32 /s cryptext.dll
regsvr32 /s inseng.dll
regsvr32 /s iesetup.dll /i
regsvr32 /s cryptdlg.dll
regsvr32 /s actxprxy.dll
regsvr32 /s dispex.dll
regsvr32 /s occache.dll
regsvr32 /s iepeers.dll
regsvr32 /s urlmon.dll /i
regsvr32 /s cdfview.dll
regsvr32 /s webcheck.dll
regsvr32 /s mobsync.dll
regsvr32 /s pngfilt.dll
regsvr32 /s licmgr10.dll
regsvr32 /s hhctrl.ocx
regsvr32 /s inetcfg.dll
regsvr32 /s trialoc.dll
regsvr32 /s tdc.ocx
regsvr32 /s MSR2C.DLL
regsvr32 /s msident.dll
regsvr32 /s msieftp.dll
regsvr32 /s xmsconf.ocx
regsvr32 /s ils.dll
regsvr32 /s msoeacct.dll
regsvr32 /s wab32.dll
regsvr32 /s wabimp.dll
regsvr32 /s wabfind.dll
regsvr32 /s oemiglib.dll
regsvr32 /s directdb.dll
regsvr32 /s inetcomm.dll
regsvr32 /s msoe.dll
regsvr32 /s oeimport.dll
regsvr32 /s msdxm.ocx
regsvr32 /s dxmasf.dll
regsvr32 /s laprxy.dll
regsvr32 /s l3codecx.ax
regsvr32 /s acelpdec.ax
regsvr32 /s mpg4ds32.ax
regsvr32 /s danim.dll
regsvr32 /s Daxctle.ocx
regsvr32 /s lmrt.dll
regsvr32 /s datime.dll
regsvr32 /s dxtrans.dll
regsvr32 /s dxtmsft.dll
regsvr32 /s wshom.ocx
regsvr32 /s wshext.dll
regsvr32 /s vbscript.dll
regsvr32 /s scrrun.dll mstinit.exe /setup
regsvr32 /s msnsspc.dll /SspcCreateSspiReg
regsvr32 /s msapsspc.dll /SspcCreateSspiReg
echo.
echo.
echo 修复成功!任意键退出!
pause>nul
如果是组件的注册问题,用这种方法解决了。
方法三:
我的机器用了上面的方法没有解决问题,用下面的方法解决了问题,大家不妨可以试试:打开Internet Explorer “工具”菜单栏中的“选项”一栏,单击“安全”栏中的“自定义级别”选项卡,将第三项“对没有标记为安全的activex控件进行初始化和脚本运行”设置成“启用”即可。