从.NET中的Excel读取数据时dll文件错误 [英] Error in dll file while reading data from excel in .NET
问题描述
我试图通过.NET中的Web应用程序从excel读取数据,为此,当我右键单击网站名称"并添加dll文件Interop时,我通过添加引用"选项添加了Interop.Microsoft.Office.Interop.Excel. .Microsoft.Office.Interop.Excel.dll到垃圾箱.然后,我写了下面的代码(从Internet),但是出现了8个错误.这8个错误是相同的,它们是错误
I am trying to read data from excel through a web application in .NET and for that I added Interop.Microsoft.Office.Interop.Excel by Add Reference option when I right click on the Website Name and added the dll file Interop.Microsoft.Office.Interop.Excel.dll to the bin. Then I had written the below code (from internet) but 8 errors are appearing. The 8 errors are same and they are "Error
1 The type ''Microsoft.Office.Interop.Excel.Application'' exists in both <br />
<br />
''c:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll'' and ''c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727<br />
<br />
\Temporary ASP.NET Files\website2\0f529463\23e4676f\assembly\dl3\72f9194e\00700289_abf9c601\Interop.Microsoft.Office.Interop.Excel.DLL'' C:\Documents and <br />
<br />
Settings\Administrator\My Documents\Visual Studio 2008\WebSites\WebSite2\Default.aspx.cs 57 15 C:\...\WebSite2\"
C#代码为:-
The C# code is: -
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing; // added
//using Microsoft.Office.Interop.Excel;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack != true)
{
//func();
//_excel = new Excel.ApplicationClass();
//Excel.Workbook workbook = _excel.Workbooks.Add(Type.Missing);
//_excel.Visible = true;
//Worksheet ws = (Worksheet)_excel.ActiveSheet;
//ws.Activate();
}
}
protected void btnMorningReport_Click(object sender, EventArgs e)
{
string strMorningReportFile;
strMorningReportFile = txtFilePathMorningReport.Value;
lblMorningReport.Text = strMorningReportFile; //temporary
}
protected void btnFollowupReport_Click(object sender, EventArgs e)
{
string strFollowupReportFile;
strFollowupReportFile = txtFilePathFollowupReport.Value;
lblFollowupReport.Text = strFollowupReportFile; //temporary
}
protected void btnEveningReport_Click(object sender, EventArgs e)
{
string strEveningReportFile;
strEveningReportFile = txtFilePathEveningReport.Value;
lblEveningReport.Text = strEveningReportFile; //temporary
}
public void func()
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell.
oSheet.Cells[1, 1] = "First Name";
oSheet.Cells[1, 2] = "Last Name";
oSheet.Cells[1, 3] = "Full Name";
oSheet.Cells[1, 4] = "Salary";
//Format A1:D1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "D1").Font.Bold = true;
oSheet.get_Range("A1", "D1").VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
// Create an array to multiple values at once.
string[,] saNames = new string[5, 2];
saNames[0, 0] = "John";
saNames[0, 1] = "Smith";
saNames[1, 0] = "Tom";
saNames[1, 1] = "Brown";
saNames[2, 0] = "Sue";
saNames[2, 1] = "Thomas";
saNames[3, 0] = "Jane";
saNames[3, 1] = "Jones";
saNames[4, 0] = "Adam";
saNames[4, 1] = "Johnson";
//Fill A2:B6 with an array of values (First and Last Names).
oSheet.get_Range("A2", "B6").Value2 = saNames;
//Fill C2:C6 with a relative formula (=A2 & " " & B2).
oRng = oSheet.get_Range("C2", "C6");
oRng.Formula = "=A2 & \" \" & B2";
//Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.get_Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
//AutoFit columns A:D.
oRng = oSheet.get_Range("A1", "D1");
oRng.EntireColumn.AutoFit();
//Manipulate a variable number of columns for Quarterly Sales Data.
//DisplayQuarterlySales(oSheet);
//Make sure Excel is visible and give the user control
//of Microsoft Excel''s lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
}
Please some one help me resolve this problem.
推荐答案
0.00; //自动调整列A:D. oRng = oSheet.get_Range("A1","D1"); oRng.EntireColumn.AutoFit(); //为季度销售数据"处理可变的列数. //DisplayQuarterlySales(oSheet); //确保Excel是可见的并授予用户控制权 //Microsoft Excel的生命周期. oXL.Visible = true; oXL.UserControl = true; } }
0.00"; //AutoFit columns A:D. oRng = oSheet.get_Range("A1", "D1"); oRng.EntireColumn.AutoFit(); //Manipulate a variable number of columns for Quarterly Sales Data. //DisplayQuarterlySales(oSheet); //Make sure Excel is visible and give the user control //of Microsoft Excel''s lifetime. oXL.Visible = true; oXL.UserControl = true; } }
请有人帮我解决这个问题.
Please some one help me resolve this problem.
请参阅以下类似问题:
http://social.msdn.microsoft.com /forums/zh-CN/csharpgeneral/thread/3091f8d5-4c3c-487c-88d2-da45cc15c250/ [ http://stackoverflow.com/questions/371426/asp-net-error-the-type-foo-exists-in-both-temp1-dll-and-temp2-dll [ http://stackoverflow.com/questions/1057853/the-type-type-全部存在dlls [ ^ ]
See the following similar questions:
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/3091f8d5-4c3c-487c-88d2-da45cc15c250/[^]
http://stackoverflow.com/questions/371426/asp-net-error-the-type-foo-exists-in-both-temp1-dll-and-temp2-dll[^]
http://stackoverflow.com/questions/1057853/the-type-type-exists-in-both-dlls[^]
这篇关于从.NET中的Excel读取数据时dll文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!