跪求:asp.net导出Excel文件错误解决方案

姓孙名悟空 2010-11-15 04:02:07
Server Error in '/web' Application.
--------------------------------------------------------------------------------

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

以上的导出Excel时出现的错误信息.
系统在局域网内使用,导入Excel文档功能正常,机器安装的是xp 企业版系统 IIS 5,Office2007,在导出Excel时出现以上错误,我已经进入dcomcnfg里对window office excel 应用程序对以下多个用户进行了权限分配 aspnet, everyone ,service 等
...全文
492 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
shichao102471077 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 net_lover 的回复:]
建议不要使用Microsoft.Office.Interop这种方法,服务器会有很多限制的,除非服务器是自己的,可以采用xml格式的生成方法
参见
http://dotnet.aspx.cc/article/e9e795ab-8fb5-47e8-a586-2c943a8e5408/read.aspx
[/Quote]

学习了。。
孟子E章 2010-11-16
  • 打赏
  • 举报
回复
建议不要使用Microsoft.Office.Interop这种方法,服务器会有很多限制的,除非服务器是自己的,可以采用xml格式的生成方法
参见
http://dotnet.aspx.cc/article/e9e795ab-8fb5-47e8-a586-2c943a8e5408/read.aspx
xinshouno7 2010-11-16
  • 打赏
  • 举报
回复
是不是需要打开啊
仅供参考啊。呵呵

object obj=Missing.Value;
Workbook ExlWorkBook=new Microsoft.Office.Interop.Excel.Application().Workbooks.Open(strFileName, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj);
loadown 2010-11-16
  • 打赏
  • 举报
回复
最简单的方法,发送EXCEL文件头给客户端,然后数据用HTML的表格形式直接RESPONSE给客户端就OK了,客户端打开该XLS文件的话,会自动转换为标准EXCEL文件,省时省力还不用EXECEL组件。
姓孙名悟空 2010-11-15
  • 打赏
  • 举报
回复
public void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
{
return;
}
long rowNum = tmpDataTable.Rows.Count;//行数
int columnNum = tmpDataTable.Columns.Count;//列数
Excel.Application m_xlApp = new Excel.Application();
m_xlApp.DisplayAlerts = false;//不显示更改提示
m_xlApp.Visible = false;

Excel.Workbooks workbooks = m_xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1

try
{
if (rowNum > 65536)//单张Excel表格最大行数
{
long pageRows = 65535;//定义每页显示的行数,行数必须小于65536
int scount = (int)(rowNum / pageRows);//导出数据生成的表单数
if (scount * pageRows < rowNum)//当总行数不被pageRows整除时,经过四舍五入可能页数不准
{
scount = scount + 1;
}
for (int sc = 1; sc <= scount; sc++)
{
if (sc > 1)
{
object missing = System.Reflection.Missing.Value;
worksheet = (Excel.Worksheet)workbook.Worksheets.Add(
missing, missing, missing, missing);//添加一个sheet
}
else
{
worksheet = (Excel.Worksheet)workbook.Worksheets[sc];//取得sheet1
}
string[,] datas = new string[pageRows + 1, columnNum];

for (int i = 0; i < columnNum; i++) //写入字段
{
datas[0, i] = tmpDataTable.Columns[i].Caption;//表头信息
}
Excel.Range range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnNum]);
range.Interior.ColorIndex = 15;//15代表灰色
range.Font.Bold = true;
range.Font.Size = 9;

int init = int.Parse(((sc - 1) * pageRows).ToString());
int r = 0;
int index = 0;
int result;
if (pageRows * sc >= rowNum)
{
result = (int)rowNum;
}
else
{
result = int.Parse((pageRows * sc).ToString());
}

for (r = init; r < result; r++)
{
index = index + 1;
for (int i = 0; i < columnNum; i++)
{
object obj = tmpDataTable.Rows[r][tmpDataTable.Columns[i].ToString()];
datas[index, i] = obj == null ? "" : "'" + obj.ToString().Trim();//在obj.ToString()前加单引号是为了防止自动转化格式
}
//System.Windows.Forms.Application.DoEvents();
//添加进度条
}

Excel.Range fchR = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[index + 1, columnNum]);
fchR.Value2 = datas;
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
m_xlApp.WindowState = Excel.XlWindowState.xlMaximized;//Sheet表最大化
range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[index + 1, columnNum]);
//range.Interior.ColorIndex = 15;//15代表灰色
range.Font.Size = 9;
range.RowHeight = 14.25;
range.Borders.LineStyle = 1;
range.HorizontalAlignment = 1;
}
}
else
{
string[,] datas = new string[rowNum + 1, columnNum];
for (int i = 0; i < columnNum; i++) //写入字段
{
datas[0, i] = tmpDataTable.Columns[i].Caption;
}
Excel.Range range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnNum]);
range.Interior.ColorIndex = 15;//15代表灰色
range.Font.Bold = true;
range.Font.Size = 9;

int r = 0;
for (r = 0; r < rowNum; r++)
{
for (int i = 0; i < columnNum; i++)
{
object obj = tmpDataTable.Rows[r][tmpDataTable.Columns[i].ToString()];
datas[r + 1, i] = obj == null ? "" : "'" + obj.ToString().Trim();//在obj.ToString()前加单引号是为了防止自动转化格式
}
//System.Windows.Forms.Application.DoEvents();
//添加进度条
}
Excel.Range fchR = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]);
fchR.Value2 = datas;

worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
m_xlApp.WindowState = Excel.XlWindowState.xlMaximized;

range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]);
//range.Interior.ColorIndex = 15;//15代表灰色
range.Font.Size = 9;
range.RowHeight = 14.25;
range.Borders.LineStyle = 1;
range.HorizontalAlignment = 1;
}
workbook.Saved = true;
workbook.SaveCopyAs("" + strFileName + "");
}
catch (Exception ex)
{
//MessageBox.Show("导出异常:" + ex.Message, "导出异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
EndReport(m_xlApp);
}
}


/// <summary>
/// 退出报表时关闭Excel和清理垃圾Excel进程
/// </summary>
private void EndReport(Excel.Application m_xlApp)
{
object missing = System.Reflection.Missing.Value;
try
{
m_xlApp.Workbooks.Close();
m_xlApp.Workbooks.Application.Quit();
m_xlApp.Application.Quit();
m_xlApp.Quit();
}
catch { }
finally
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp.Workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp.Application);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp);
m_xlApp = null;
}
catch { }
try
{
//清理垃圾进程
this.KillProcessThread();
}
catch { }
GC.Collect();
}
}
这是导出的C#代码
姓孙名悟空 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liuxueqian307 的回复:]

你先看下,在程序中的哪个地方出的错
[/Quote]

Excel.Application m_xlApp = new Excel.Application(); //导出运行到这一行代码时报错
liuxueqian307 2010-11-15
  • 打赏
  • 举报
回复
你先看下,在程序中的哪个地方出的错
gxlgd 2010-11-15
  • 打赏
  • 举报
回复
英文盲。。。。。
帮你顶起。。。
姓孙名悟空 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 myhope88 的回复:]

人家都提示未授权啦,你得从这方面下手嘛,是不是跟操作系统和office版本不同有关呢
[/Quote]
应该是的,这个在我们公司的服务器上运行正常,到了客户那边一直出这个问题,我也给window excel application分配过权限,还是一直出现这个错误,导入功能是正常的
myhope88 2010-11-15
  • 打赏
  • 举报
回复
人家都提示未授权啦,你得从这方面下手嘛,是不是跟操作系统和office版本不同有关呢
neo33233 2010-11-15
  • 打赏
  • 举报
回复
帮顶一下,貌似office组件的问题吗,猜的呵呵

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧