Asp.Net 操作Office笔记

本文介绍了使用ASP.NET和Interop DLL操作Word文档的方法,包括如何填充文档中的书签位置、设置查询条件并从数据库中获取数据填充到Word表格中。此外,还提供了服务器上部署所需的office环境配置步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

去年和同学做联通的一个OA项目时
涉及到asp.net操作word的问题
今年陆续又碰到操作excel、word等office文档
使用的都是Interop相关dll,打开office相关进程进行操作
为免忘记,将相关tip整理如下:
1. 引用文件, 几个dll打包了
2. 操作word代码示例:
using  ImportExportToOffice;
using  Microsoft.ApplicationBlocks.Data;

  
protected   void  btnJianJie_Click( object  sender, EventArgs e)
    {
        
string  sYear  =  DropDownList2.SelectedValue;
        
string  sMonth  =  DropDownList1.SelectedValue;
        
string  sDate1  =  String.Format( " {0}-{1}-{2} " , sYear, sMonth,  " 1 " );
        
string  sDate2  =  String.Format( " {0}-{1}-{2} " , sYear, sMonth, System.DateTime.DaysInMonth( int .Parse(sYear),  int .Parse(sMonth)).ToString());
        word 
=   new  ImportExportToWord();

        
string  sTmpUrl  =   " ../Report/ "   +  Guid.NewGuid().ToString()  +   " .Doc " ;
        
string  sTmpFile  =  Server.MapPath(sTmpUrl);
        
string  sModelDoc  =  Server.MapPath( " ../Report/国际漫游简报模板.doc " );
        System.IO.File.Copy(sModelDoc, sTmpFile, 
true );
        
try
        {
            word.Open(sTmpFile);

            
object  sBookMark  =   "" ;

            
// GSM 来访前十 分公司
            sBookMark  =   " GSM_1 " ;
            
if  (word.Document.Bookmarks.Exists(sBookMark.ToString()))
            {
                word.Document.Bookmarks.get_Item(
ref  sBookMark).Select();

                
string  sSql  =   " SELECT TOP 10 t1.ToCarrier, COUNT(t1.ToCarrier) AS Num, t2.ShortName AS Name "   +
                    
"  FROM BiteGSM t1 INNER JOIN "   +
                    
"  BranchInfo t2 ON t1.ToCarrier = t2.ID "   +
                    
"  INNER JOIN DispatchInfo t3 ON t1.DispatchID = t3.ID "   +
                    
"  Where t3.SendTime >=' "   +  sDate1  +   " ' "   +
                    
"  And t3.SendTime <=' "   +  sDate2  +   " ' "   +
                    
"  And t1.SimType=1 "   +
                    
"  GROUP BY t1.ToCarrier, t2.ShortName "   +
                    
"  ORDER BY COUNT(t1.ToCarrier) DESC " ;
                SqlDataReader dr 
=  SqlHelper.ExecuteReader(Connection.ConnectionString, CommandType.Text, sSql);
                
while  (dr.Read())
                {
                    word.Application.Selection.TypeText(dr[
" Name " ].ToString());
                    word.GoToRightCell();
                    word.Application.Selection.TypeText(dr[
" Num " ].ToString());
                    word.GoToLeftCell();
                    word.GoToDownCell();
                }
                dr.Close();
            }

            
// CDMA 来访前十 分公司
            sBookMark  =   " CDMA_1 " ;
            
if  (word.Document.Bookmarks.Exists(sBookMark.ToString()))
            {
                word.Document.Bookmarks.get_Item(
ref  sBookMark).Select();

                
string  sSql  =   " SELECT TOP 10 t1.ToCarrier, COUNT(t1.ToCarrier) AS Num, t2.ShortName AS Name "   +
                    
"  FROM BiteGSM t1 INNER JOIN "   +
                    
"  BranchInfo t2 ON t1.ToCarrier = t2.ID "   +
                    
"  INNER JOIN DispatchInfo t3 ON t1.DispatchID = t3.ID "   +
                    
"  Where t3.SendTime >=' "   +  sDate1  +   " ' "   +
                    
"  And t3.SendTime <=' "   +  sDate2  +   " ' "   +
                    
"  And t1.SimType=2 "   +
                    
"  GROUP BY t1.ToCarrier, t2.ShortName "   +
                    
"  ORDER BY COUNT(t1.ToCarrier) DESC " ;
                SqlDataReader dr 
=  SqlHelper.ExecuteReader(Connection.ConnectionString, CommandType.Text, sSql);
                
while  (dr.Read())
                {
                    word.Application.Selection.TypeText(dr[
" Name " ].ToString());
                    word.GoToRightCell();
                    word.Application.Selection.TypeText(dr[
" Num " ].ToString());
                    word.GoToLeftCell();
                    word.GoToDownCell();
                }
                dr.Close();
            }

            
// GSM 去访前十 分公司
            sBookMark  =   " GSM1 " ;
            
if  (word.Document.Bookmarks.Exists(sBookMark.ToString()))
            {
                word.Document.Bookmarks.get_Item(
ref  sBookMark).Select();

                
string  sSql  =   " SELECT TOP 10 t1.ToCarrier, COUNT(t1.ToCarrier) AS Num, t2.CarrierName AS Name "   +
                    
"  FROM Unicom2TSTT t1 INNER JOIN "   +
                    
"  CarrierInfo t2 ON t1.ToCarrier = t2.ID "   +
                    
"  INNER JOIN DispatchInfo t3 ON t1.DispatchID = t3.ID "   +
                    
"  Where t3.SendTime >=' "   +  sDate1  +   " ' "   +
                    
"  And t3.SendTime <=' "   +  sDate2  +   " ' "   +
                    
"  And t1.SimType=1 "   +
                    
"  GROUP BY t1.ToCarrier, t2.CarrierName "   +
                    
"  ORDER BY COUNT(t1.ToCarrier) DESC " ;
                SqlDataReader dr 
=  SqlHelper.ExecuteReader(Connection.ConnectionString, CommandType.Text, sSql);
                
while  (dr.Read())
                {
                    word.Application.Selection.TypeText(dr[
" Name " ].ToString());
                    word.GoToRightCell();
                    word.Application.Selection.TypeText(dr[
" Num " ].ToString());
                    word.GoToLeftCell();
                    word.GoToDownCell();
                }
                dr.Close();
            }

            
// CDMA 去访前十 分公司
            sBookMark  =   " CDMA1 " ;
            
if  (word.Document.Bookmarks.Exists(sBookMark.ToString()))
            {
                word.Document.Bookmarks.get_Item(
ref  sBookMark).Select();

                
string  sSql  =   " SELECT TOP 10 t1.ToCarrier, COUNT(t1.ToCarrier) AS Num, t2.CarrierName AS Name "   +
                    
"  FROM Unicom2TSTT t1 INNER JOIN "   +
                    
"  CarrierInfo t2 ON t1.ToCarrier = t2.ID "   +
                    
"  INNER JOIN DispatchInfo t3 ON t1.DispatchID = t3.ID "   +
                    
"  Where t3.SendTime >=' "   +  sDate1  +   " ' "   +
                    
"  And t3.SendTime <=' "   +  sDate2  +   " ' "   +
                    
"  And t1.SimType=2 "   +
                    
"  GROUP BY t1.ToCarrier, t2.CarrierName "   +
                    
"  ORDER BY COUNT(t1.ToCarrier) DESC " ;
                SqlDataReader dr 
=  SqlHelper.ExecuteReader(Connection.ConnectionString, CommandType.Text, sSql);
                
while  (dr.Read())
                {
                    word.Application.Selection.TypeText(dr[
" Name " ].ToString());
                    word.GoToRightCell();
                    word.Application.Selection.TypeText(dr[
" Num " ].ToString());
                    word.GoToLeftCell();
                    word.GoToDownCell();
                }
                dr.Close();
            }

            word.SaveAs(sTmpFile);
            word.Quit();
            SysUtil.Log(UserContext.GetUserContext(), FunctionContext.GetFunctionContext(),OperType.其它, LogLevel.信息, 
" 生成国际漫游简报成功 " );
            Response.Write(
" <script>window.open(\ ""  + sTmpUrl +  " \ " )</script> " );
        }
        
catch  (Exception err)
        {
            
try
            {
                word.Quit();
            }
            
catch
            {
            }
            SysUtil.Log(UserContext.GetUserContext(), FunctionContext.GetFunctionContext(),OperType.其它, LogLevel.信息, 
" 生成国际漫游简报失败,错误信息: "   +  err.Message);
        }
    }

3.部署相关: 确保服务器正确安装office 2k3
运行后若office进程起不来,如下检查:
1.运行Dcomcnfg.exe
2.组件服务――计算机――我的电脑――DCOM配置――找到microsoft word 文档
3.点击属性
4.选择“安全性”
5.选定“使用自定义访问权限”和“使用自定义启动权限”
6.分别编辑权限,添加Everyone(ASPNET,VS Developers,Debugger User)
7.选择“身份标识”,在选定“交互式用户” 即可
8.在Web.config里加 <identity impersonate="true"/>

转载于:https://www.cnblogs.com/calmzeal/archive/2007/09/04/ASPNETOFFICE.html

作为ASP.NET项目的开发者,在应对用户的Word、Excel文档时,您是否迫切希望Visual Studio工具箱里能有现成的Word、Excel标准ASP.NET控件可用就太好了?现在梦想已经成真,想在网页上显示编辑Word、Excel文档并且调用微软Office的强大功能,就直接从工具箱拖动一个PageOffice控件到Web页面上就可以了,就和使用常用的TextBox控件一样简单方便。 PageOffice是什么? PageOffice for ASP.NET是一款非常优秀的专业的集成微软OFFICE功能的ASP.NET控件平台,能够帮助Web开发人员轻松实现在网页中嵌入运行微软Office的特殊功能。PageOffice把微软复杂的Office编程接口(COM API) 有效地整合于 ASP.NET 环境,通过简化的.NET类库接口为开发者提供了在Web项目里获得Office众多强大功能的机会,并且能够同时利用来自 .NET Framework 的开发效率和软件功能。 集成PageOffice不但能够实现在线编辑、保存真正的Office文档,而且还可以轻松实现Word、Excel文档的动态数据填充、格式控制和用户输入提交的高级复杂功能。只要集成PageOffice,您将能够轻松应对在Web项目开发中遇见的绝大部分和Office文档有关的功能和难题。您只需把精力集中在项目业务逻辑上,Office技术问题就交给PageOffice实现解决吧。 PageOffice除了提供Word/Excel动态数据填充,格式控制,Word/Excel用户输入提交,Word/Excel/PowerPoint/WPS等Office文档的在线打开、只读浏览、编辑、保存等功能外,还给在线协同办公提供了强大的支持功能:可编辑区域控制,强制痕迹保留,并发编辑控制,手写批注,手写签名,圈阅签字,电子印章、动态模板套红等。 专业的MSDN风格的开发帮助,上百的示例代码,确保您的开发效率事半功倍。 运行环境 服务器端:Windows2003、Windows2008、Windows2012及Windows XP、Windows7、Windows8等(包括64位版本);.Netframework2.0及以上版本。 客户端:Windows XP、Windows Vista、Windows7、Windows8及以上版本(包括64位版本);Microsoft Office2003、2007、2010、2013;IE 6.0、7.0、8.0、9.0、10.0。 名称:PageOffice for ASP.NET专业版 2.0 下载:http://www.zhuozhengsoft.com/down/PageOffice_ASP.NET.rar 大小:13.8MB 版本:专业版 2.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值