一个很简单的操作excel的例子

第三天了,总于解决了
是web.config中的一句话去掉就行了
    <identity impersonate="true"/>

代码不能运行产生问题:
我的机子上可以,服务器不能运行。现象是oledb不能Open();总是出错

贴出代码:
None.gif          private   void  SaveToExcel()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            
string urlPath = HttpContext.Current.Request.ApplicationPath + "/Temp/";
InBlock.gif
InBlock.gif            
string physicPath = HttpContext.Current.Server.MapPath(urlPath);
InBlock.gif
InBlock.gif            
string fileName = Guid.NewGuid() + ".Xls"
InBlock.gif
InBlock.gif            
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + physicPath + fileName +";Extended Properties=Excel 8.0;";
InBlock.gif
InBlock.gif            OleDbConnection objConn 
= new OleDbConnection(connString);
InBlock.gif            OleDbCommand objCmd 
= new OleDbCommand();
InBlock.gif            objCmd.Connection 
= objConn;
InBlock.gif            objConn.Open();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
//建立表结构
InBlock.gif

InBlock.gif            objCmd.CommandText 
= @"CREATE TABLE TBL_Customer (CustomerName varchar,CustomerNo varchar)";
InBlock.gif
InBlock.gif            objCmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif            objCmd.CommandText 
= "INSERT INTO TBL_Customer(CustomerName, CustomerNo) VALUES ('毛小华', 'good')";
InBlock.gif
InBlock.gif            objCmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif            
//提供下载
InBlock.gif

InBlock.gif            objCmd.Dispose();
InBlock.gif            objConn.Dispose();
InBlock.gif            
InBlock.gif
InBlock.gif            HttpResponse response 
= HttpContext.Current.Response; 
InBlock.gif            response.Clear();
InBlock.gif            response.WriteFile(physicPath 
+ fileName);
InBlock.gif            
string httpHeader="attachment;filename=backup.Xls";
InBlock.gif            response.AppendHeader(
"Content-Disposition", httpHeader);
InBlock.gif            response.Flush();
InBlock.gif
InBlock.gif            System.IO.File.Delete(physicPath 
+ fileName);//删除临时文件
InBlock.gif
            response.End();        
InBlock.gif        
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  Button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//StartOutPort();
InBlock.gif
            
InBlock.gif            SaveToExcel();
ExpandedBlockEnd.gif        }


处理步骤,查了很多文章,做了很多步骤:
1、怀疑权限,把Temp的权限设置了完全,不行!
2、怀疑mdac,jet等engine有问提,就重装了,不行!
3、索性sql sp3, 和 window  sp4, 等所有可安装的补丁重新装一边,还是不行。
4、有说IIS, 和匡架有可能,就把iis和frame work 1.1都重新安装了一边,还是不行,。
5、郁闷,睡觉了再想,重新建新工程 , 哈哈 ok, 拷入旧ws.config  出错。
呵呵,一个一个比较,就得到    <identity impersonate="true"/>是有问题的。

总结:
我的机子是fat32的权限要求不高 能够运行。
服务器是ntfs的权限要求高,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值