首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 操作Excel文件解决方法

2012-03-31 
C# 操作Excel文件用C#实现一下功能:根据给定的完整文件名FilePath的Excel文件:首先判断文件是否存在,如果

C# 操作Excel文件
用C#实现一下功能:
  根据给定的完整文件名FilePath的Excel文件:
  首先判断文件是否存在,如果存在打开它向文件的表末尾添加新数据,如果不存在则创建他并打开向它的表中田间数据。

[解决办法]

探讨
用C#实现一下功能:
    根据给定的完整文件名FilePath的Excel文件:
        首先判断文件是否存在,如果存在打开它向文件的表末尾添加新数据,如果不存在则创建他并打开向它的表中田间数据。

[解决办法]
if (File.Exists(@"D:\\xxxx.xls"))
{
try
{
// 打开进行操作
}
catch (Exception ex)
{
MessageBox.Show("错误\r\n" + ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
Excel的打开和追加内容,你百度下就有了
[解决办法]
给你操作Excel的代码,微软的
/****************************** Module Header ******************************\
* Module Name:Program.cs
* Project:CSAutomateExcel
* Copyright (c) Microsoft Corporation.

* The CSAutomateExcel example demonstrates how to use C# codes to create an 
* Microsoft Excel instance, create a workbook, and fill data into the 
* specified range, as well as how to clean up unmanaged COM resources and 
* quit the Excel application properly.

* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.

* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
#endregion


class Program
{
static void Main(string[] args)
{
object missing = Type.Missing;
Excel.Application oXL = null;
Excel.Workbooks oWBs = null;
Excel.Workbook oWB = null;
Excel.Worksheet oSheet = null;
Excel.Range oCells = null;
Excel.Range oRng1 = null;
Excel.Range oRng2 = null;


/////////////////////////////////////////////////////////////////////
// Create an instance of Microsoft Excel and make it invisible.
// 

oXL = new Excel.Application();
//Visible = true; //如果只想用程序控制该excel而不想让用户操作时候
oXL.Visible = false;
Console.WriteLine("Excel.Application is started");


/////////////////////////////////////////////////////////////////////
// Create a new Workbook.
// 

oWBs = oXL.Workbooks;
oWB = oWBs.Add(missing);
Console.WriteLine("A new workbook is created");


/////////////////////////////////////////////////////////////////////
// Get the active Worksheet and set its name.
// 

oSheet = oWB.ActiveSheet as Excel.Worksheet;
oSheet.Name = "Report";
Console.WriteLine("The active worksheet is renamed as Report");


/////////////////////////////////////////////////////////////////////
// Fill data into the worksheet's cells.
// 



Console.WriteLine("Filling data into the worksheet ...");

// Set the column header
oCells = oSheet.Cells;
oCells[1, 1] = "First Name";
oCells[1, 2] = "Last Name";
oCells[1, 3] = "Full Name";

// Construct an array of user names
string[,] saNames = new string[,] {
{"John", "Smith"}, 
{"Tom", "Brown"}, 
{"Sue", "Thomas"}, 
{"Jane", "Jones"}, 
{"Adam", "Johnson"}};

// Fill A2:B6 with an array of values (First and Last Names).
oRng1 = oSheet.get_Range("A2", "B6");
oRng1.Value2 = saNames;

// Fill C2:C6 with a relative formula (=A2 & " " & B2).
oRng2 = oSheet.get_Range("C2", "C6");
oRng2.Formula = "=A2 & \" \" & B2";


/////////////////////////////////////////////////////////////////////
// Save the workbook as a xlsx file and close it.
// 

Console.WriteLine("Save and close the workbook");

string fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().
Location) + "\\MSDN.xls";
oWB.SaveAs(fileName,missing,
missing, missing, missing, missing,
Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
oWB.Close(missing, missing, missing);


/////////////////////////////////////////////////////////////////////
// Quit the Excel application.
// 

Console.WriteLine("Quit the Excel application");

// Excel will stick around after Quit if it is not under user control 
// and there are outstanding references. When Excel is started or 
// attached programmatically and Excel.Application.Visible = false, 
// Excel.Application.UserControl is false. The UserControl property 
// can be explicitly set to True which should force the application 
// to terminate when Quit is called, regardless of outstanding 
// references.
oXL.UserControl = true;

oXL.Quit();


/////////////////////////////////////////////////////////////////////
// Clean up the unmanaged COM resources.
// 

// Explicitly call Marshal.FinalReleaseComObject on all accessor 
// objects. See http://support.microsoft.com/kb/317109.
Marshal.FinalReleaseComObject(oRng2);
oRng2 = null;
Marshal.FinalReleaseComObject(oRng1);
oRng1 = null;
Marshal.FinalReleaseComObject(oCells);
oCells = null;
Marshal.FinalReleaseComObject(oSheet);
oSheet = null;
Marshal.FinalReleaseComObject(oWB);
oWB = null;
Marshal.FinalReleaseComObject(oWBs);
oWBs = null;
Marshal.FinalReleaseComObject(oXL);
oXL = null;

// [-and/or-]

// Force a garbage collection as soon as the calling function is off 
// the stack (at which point these objects are no longer rooted) and 
// then call GC.WaitForPendingFinalizers.
GC.Collect();
GC.WaitForPendingFinalizers();
// GC needs to be called twice in order to get the Finalizers called 
// - the first time in, it simply makes a list of what is to be 
// finalized, the second time in, it actually the finalizing. Only 
// then will the object do its automatic ReleaseComObject.


GC.Collect();
GC.WaitForPendingFinalizers();
}
}

然后附加说明:
一、添加引用
添加com组件(Microsoft Office 11.0 Object Library )命名空间为Microsoft.Office.Interop.Excel
即Microsoft.Office.Interop.Excel.dll 你必须安装office
添加Excel.exe引用默认路径为C:\Program Files\Microsoft Office\OFFICE11\Excel.exe

[解决办法]
帮顶···············
[解决办法]
。。。。有点复杂

热点排行