I'm writing an application which works with excel files. I need a feature to delete a sheet. I have to use an assembly Microsoft.Office.Interop.Excel.dll.
我正在写一个可以处理excel文件的应用程序。我需要一个功能来删除表格。我必须使用汇编语言。office.interop . excel.dll。
It's running fine on developer machine but when I try to deploy it on server I'm getting an error:
它在开发人员机器上运行良好,但当我尝试在服务器上部署它时,我就会出错:
Could not load file or assembly 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies
无法加载文件或程序集的office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c,或其依赖项之一?
I understand that problem occurs when MS Office is not installed on a machine. Customer don't want to install and buy MS Office on a server not at any price.
我理解当MS Office不安装在机器上时出现的问题。客户不希望在服务器上安装和购买MS Office,无论价格如何。
I install "Redistributable Primary Interop Assemblies" on developer machine as advised here: http://forums.asp.net/t/1530230.aspx/1 and compile my project again.
我在开发人员机器上安装了“可重新分发的主互操作程序集”,请参见这里的建议:http://forums.asp.net/t/15300.aspx/1,然后再次编译我的项目。
Code sample:
代码示例:
public bool DeleteSheet(string tableName)
{
Excel.Application app = null;
Excel.Workbooks wbks = null;
Excel._Workbook _wbk = null;
Excel.Sheets shs = null;
bool found = false;
try
{
app = new Excel.Application();
app.Visible = false;
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
wbks = app.Workbooks;
_wbk = wbks.Add(xlsfile);
shs = _wbk.Sheets;
int nSheets = shs.Count;
for (int i = 1; i <= nSheets; i++)
{
Excel._Worksheet _iSheet = (Excel._Worksheet)shs.get_Item(i);
if (_iSheet.Name == tableName)
{
_iSheet.Delete();
found = true;
Marshal.ReleaseComObject(_iSheet);
break;
}
Marshal.ReleaseComObject(_iSheet);
}
if (!found)
throw new Exception(string.Format("Table \"{0}\" was't found", tableName));
_wbk.SaveAs(connect, _wbk.FileFormat, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
finally
{
_wbk.Close(null, null, null);
wbks.Close();
app.Quit();
Marshal.ReleaseComObject(shs);
Marshal.ReleaseComObject(_wbk);
Marshal.ReleaseComObject(wbks);
Marshal.ReleaseComObject(app);
}
return true;
}
An exception
一个例外
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
使用CLSID {00024500-0000-0000- c000 - 0000000000000046}检索COM类工厂失败,原因是以下错误:80040154类未注册(HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)除外)。
occurs on the line
发生在直线上
app = new Excel.Application();
Can anyone advise on how to get this feature working successfully?
有人能建议如何使这个功能成功工作吗?
4 个解决方案
#1
53
You can't use Microsoft.Office.Interop.Excel without having ms office installed.
你不能使用Microsoft.Office.Interop。没有安装ms office的Excel。
Just search in google for some libraries, which allows to modify xls or xlsx:
在谷歌中搜索一些库,可以修改xls或xlsx:
- http://code.google.com/p/excellibrary/
- http://code.google.com/p/excellibrary/
- http://simpleooxml.codeplex.com/ (only xlsx)
- http://simpleooxml.codeplex.com/(只有xlsx)
#2
2
you can create a service and generate excel on server and then allow clients download excel. cos buying excel license for 1000 ppl, it is better to have one license for server.
您可以创建一个服务并在服务器上生成excel,然后允许客户下载excel。因为购买excel许可证需要1000 ppl,所以最好有一个服务器许可证。
hope that helps.
希望有帮助。
#3
1
If the "Customer don't want to install and buy MS Office on a server not at any price", then you cannot use Excel ... But I cannot get the trick: it's all about one basic Office licence which costs something like 150 USD ... And I guess that spending time finding an alternative will cost by far more than this amount!
如果“客户不希望在服务器上安装并购买MS Office,且价格不高”,那么您就不能使用Excel……但我搞不懂:这都是一张基本的办公执照,大约150美元……而且我猜花时间去寻找一个替代方案的花费将远远超过这个数目!
#4
0
Look for GSpread.NET. You can work with Google Spreadsheets by using API from Microsoft Excel. You don't need to rewrite old code with the new Google API usage. Just add a few row:
寻找GSpread.NET。您可以使用Microsoft Excel中的API来处理谷歌电子表格。您不需要使用新的谷歌API重写旧代码。再加几行:
Set objExcel = CreateObject("GSpreadCOM.Application");
app.MailLogon(Name, ClientIdAndSecret, ScriptId);
It's an OpenSource project and it doesn't require Office to be installed.
它是一个开源项目,不需要安装Office。
The documentation available over here http://scand.com/products/gspread/index.html
这里有http://scand.com/products/gspread/index.html的文档
#1
53
You can't use Microsoft.Office.Interop.Excel without having ms office installed.
你不能使用Microsoft.Office.Interop。没有安装ms office的Excel。
Just search in google for some libraries, which allows to modify xls or xlsx:
在谷歌中搜索一些库,可以修改xls或xlsx:
- http://code.google.com/p/excellibrary/
- http://code.google.com/p/excellibrary/
- http://simpleooxml.codeplex.com/ (only xlsx)
- http://simpleooxml.codeplex.com/(只有xlsx)
#2
2
you can create a service and generate excel on server and then allow clients download excel. cos buying excel license for 1000 ppl, it is better to have one license for server.
您可以创建一个服务并在服务器上生成excel,然后允许客户下载excel。因为购买excel许可证需要1000 ppl,所以最好有一个服务器许可证。
hope that helps.
希望有帮助。
#3
1
If the "Customer don't want to install and buy MS Office on a server not at any price", then you cannot use Excel ... But I cannot get the trick: it's all about one basic Office licence which costs something like 150 USD ... And I guess that spending time finding an alternative will cost by far more than this amount!
如果“客户不希望在服务器上安装并购买MS Office,且价格不高”,那么您就不能使用Excel……但我搞不懂:这都是一张基本的办公执照,大约150美元……而且我猜花时间去寻找一个替代方案的花费将远远超过这个数目!
#4
0
Look for GSpread.NET. You can work with Google Spreadsheets by using API from Microsoft Excel. You don't need to rewrite old code with the new Google API usage. Just add a few row:
寻找GSpread.NET。您可以使用Microsoft Excel中的API来处理谷歌电子表格。您不需要使用新的谷歌API重写旧代码。再加几行:
Set objExcel = CreateObject("GSpreadCOM.Application");
app.MailLogon(Name, ClientIdAndSecret, ScriptId);
It's an OpenSource project and it doesn't require Office to be installed.
它是一个开源项目,不需要安装Office。
The documentation available over here http://scand.com/products/gspread/index.html
这里有http://scand.com/products/gspread/index.html的文档