Joh*_*les 68 c# dll excel interop
我正在编写一个与excel文件一起使用的应用程序.我需要一个删除工作表的功能.我必须使用程序集Microsoft.Office.Interop.Excel.dll.
它在开发人员计算机上运行良好,但当我尝试在服务器上部署它时,我收到一个错误:
无法加载文件或程序集'office,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = 71e9bce111e9429c'或其中一个依赖项
我知道在计算机上未安装MS Office时会出现问题.客户不希望以任何价格在服务器上安装和购买MS Office.
我按照此处的建议在开发人员计算机上安装"Redistributable Primary Interop Assemblies":http://forums.asp.net/t/1530230.aspx/1 并再次编译我的项目.
代码示例:
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;
}
Run Code Online (Sandbox Code Playgroud)
例外
由于以下错误,检索CLSID为{00024500-0000-0000-C000-000000000046}的组件的COM类工厂失败:80040154未注册类(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG)).
发生在线上
app = new Excel.Application();
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议如何使这项功能成功运作?
use*_*979 60
如果没有安装ms office,则无法使用Microsoft.Office.Interop.Excel.
只需在谷歌搜索一些库,允许修改xls或xlsx: