我正在用C#编写一个应用程序,它打开一个用于读/写操作的Excel模板文件.我想当用户关闭应用程序时,excel应用程序进程已关闭,而不保存excel文件.多次运行应用程序后,请参阅我的任务管理器.
我使用此代码打开excel文件:
public Excel.Application excelApp = new Excel.Application();
public Excel.Workbook excelBook;
excelBook = excelApp.Workbooks.Add(@"C:/pape.xltx");
Run Code Online (Sandbox Code Playgroud)
对于数据访问,我使用以下代码:
Excel.Worksheet excelSheet = (Worksheet)(excelBook.Worksheets[1]);
excelSheet.DisplayRightToLeft = true;
Range rng;
rng = excelSheet.get_Range("C2");
rng.Value2 = txtName.Text;
Run Code Online (Sandbox Code Playgroud)
我知道有不同的方法来读取Excel文件:
Iterop
Oledb
Open Xml SDK
兼容性不是问题,因为程序将在受控环境中执行.
我的要求:
将文件读取到DataTable
/ CUstom Entitie
s(我不知道如何为对象创建动态属性/字段[列名将在Excel文件中变化])
用于DataTable/Custom Entities
使用其数据执行某些操作.
更新DataTable
操作结果
把它写回去excel file
.
哪个更简单.
如果可能的话,请告诉我自定义实体(动态地向对象添加属性/字段)
下面是我用来将数据加载到Excel工作表中的代码,但我希望在加载数据后自动调整列的大小.有谁知道自动调整列大小的最佳方法?
using Microsoft.Office.Interop;
public class ExportReport
{
public void Export()
{
Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook wb;
Excel.Worksheet ws;
Excel.Range aRange;
object m = Type.Missing;
string[,] data;
string errorMessage = string.Empty;
try
{
if (excelApp == null)
throw new Exception("EXCEL could not be started.");
// Create the workbook and worksheet.
wb = excelApp.Workbooks.Add(Office.Excel.XlWBATemplate.xlWBATWorksheet);
ws = (Office.Excel.Worksheet)wb.Worksheets[1];
if (ws == null)
throw new Exception("Could not create worksheet.");
// Set the range to fill.
aRange = ws.get_Range("A1", "E100");
if (aRange == …
Run Code Online (Sandbox Code Playgroud) 列A包含这样的数据(即频繁的空白单元格):
HEADING <-- this is A1
kfdsl
fdjgnm
fdkj
gdfkj
4353
fdjk <-- this is A9
Run Code Online (Sandbox Code Playgroud)
我希望能够获得具有数据的最后一个单元格的单元格引用.所以在上面的例子中我想返回:A9
我试过这个,但它停在第一个空白区(即返回A4
)
numofrows = destsheet.Range("A2").End(xlDown).Row - 1
Run Code Online (Sandbox Code Playgroud) 我的代码如下: -
Microsoft.Office.Interop.Excel.Application oXL = null;
Microsoft.Office.Interop.Excel.Sheets sheets;
Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add(System.Reflection.Missing.Value);
/*
* Here is the complete detail's about Workbook.Open()
*
* Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format,
* Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin,
* Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
*/
Workbook workbook = excel.Workbooks.Open(
System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
Missing.Value, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
sheets = workbook.Worksheets; …
Run Code Online (Sandbox Code Playgroud) 当我尝试使用以下代码访问Excel电子表格时,我在安装了Office 2007(版本12)的Visual Studio 2012中使用C#定义工作簿对象wrkbuk时出现"库未注册"错误
Microsoft.Office.Interop.Excel.Application excapp = new Microsoft.Office.Interop.Excel.Application();
string bookname = @"C:\Users\Public\Documents\RECRUITMENT & SELECTION\MOVEMENTS\MOVEMENTS\Miscellaneous Documents\VacanciesREAL.xls";
Workbook wrkbuk = excapp.Workbooks.Open(bookname);
Worksheet wrksht = new Worksheet();
Run Code Online (Sandbox Code Playgroud)
错误的详细信息是
System.InvalidCastException未处理HResult = -2147467262
Message =无法将类型为"Microsoft.Office.Interop.Excel.ApplicationClass"的COM对象强制转换为接口类型"Microsoft.Office.Interop.Excel._Application".此操作失败,因为由于以下错误,对IID为"{000208D5-0000-0000-C000-000000000046}"的接口的COM组件的QueryInterface调用失败:库未注册.(HRESULT的例外情况:0x8002801D(TYPE_E_LIBNOTREGISTERED)).来源= mscorlib程序
我已经在VS 2012附带的Office 14主Interop程序集中创建了对Microsoft.Office.Interop.Excel.dll的引用,并且还修复了该问题的Office 12版本.我尝试用regasm注册dll,但这也无济于事.
我可以使用Office 14和Office 12 dll在以下行上创建工作表wrksht,这样问题似乎只会影响工作簿定义.
2013年12月17日
尝试重新安装Office 2007无济于事,但发现这个解决方案,这是有效的.它在
问题是来自多个版本的Office的代码 - 我有来自Office 14的代码,可能来自VS 2012安装
我怀疑VS 2012 RC已经安装了Office 2013类型库,并且现在已经注册了重复版本.我收到了特定的错误消息
('无法转换_Application'....'TYPE_E_LIBNOTREGISTERED')
在凌乱的卸载之后,然后重新安装旧版本的Office.我通过遵循另一个论坛帖子的建议来解决它,我很遗憾找不到...基本上我在错误消息中搜索了注册表中的CLSID,所以,{00020970-0000-0000-C000-000000000046}
.它的注册表项包含两个键,其中一个键称为"TypeLib",后者又包含另一个类型库的CLSID.然后,我再次从头开始搜索注册表,找到第二个CLSID,这导致我进入相关的互操作类型库.它有两个条目.... 8.3和8.4,更大的数字对应于Office的更高版本....我删除了...并立即能够运行我的程序.
我试图使用Microsoft.Office.Interop.Excel.Workbook
类将.xls文件转换为服务器端的.xlsx文件,如下所示:
workBook.SaveAs("FILENAME_HERE", XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)). : System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, …
Run Code Online (Sandbox Code Playgroud) Microsoft.Office.Interop.Excel.WorkbookClass.SaveAs()
方法在Windows Server 2003上运行正常,也在XP上运行,但在Windows Server 2008上没有.我复制它,因为它给了所有的Dll和权限
我还尝试了网上提供的所有解决方案,例如文化设置和DCOM权限.
获得以下错误:
System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC
在
Microsoft.Office.Interop.Excel.WorkbookClass.SaveAs(Object Filename,Object FileFormat,Object Password,Object WriteResPassword,Object ReadOnlyRecommended,Object CreateBackup,XlSaveAsAccessMode AccessMode,Object ConflictResolution,Object AddToMru,Object TextCodepage,Object TextVisualLayout,Object Local)
请帮忙 ..!
我有一个Excel工作簿,其中包含一个自定义的非时间依赖的单元格函数,我使用Interop.Excel从C#WindowsForms应用程序打开.我从中读取了四个值,不执行显式更改/计算,然后从C#中关闭它.
当我尝试直接关闭(不保存)时,我收到一个保存提示.我怀疑这是因为Excel在打开时将自动重新计算解释为创建更改,即使实际上没有任何数字或公式更改.我也定了Excel.Application.Calculation = Excel.XlCalculation.xlCalculationManual
.如何阻止保存提示出现并且只是关闭而不保存?
我遇到了Excel Interop的问题.
即使我重新发布实例,Excel.exe也不会关闭.
这是我的代码:
using xl = Microsoft.Office.Interop.Excel;
xl.Application excel = new xl.Application();
excel.Visible = true;
excel.ScreenUpdating = false;
if (wordFile.Contains(".csv") || wordFile.Contains(".xls"))
{
//typeExcel become a string of the document name
string typeExcel = wordFile.ToString();
xl.Workbook workbook = excel.Workbooks.Open(typeExcel,
oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing);
object outputFileName = null;
if (wordFile.Contains(".xls"))
{
outputFileName = wordFile.Replace(".xls", ".pdf");
}
else if (wordFile.Contains(".csv"))
{
outputFileName = wordFile.Replace(".csv", ".pdf");
}
workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputFileName,
XlFixedFormatQuality.xlQualityStandard, oMissing,
oMissing, …
Run Code Online (Sandbox Code Playgroud) excel-interop ×10
c# ×8
excel ×7
.net ×3
com ×1
excel-2003 ×1
iis ×1
interop ×1
kill-process ×1
oledb ×1
openxml-sdk ×1
vb.net ×1
vba ×1
winforms ×1
xls ×1