c#如何访问打开的Excel文件?

时间:2014-10-24 12:28:34

标签: c# excel

请帮我解决这个问题!

在我的新项目中,我需要访问一些Excel文件。所有这些的名字都是已知的。打开这些文件并写入数据很容易。如果将excel对象存储到列表中,那么在程序仍在运行时,很容易重新访问已经打开的文件。请看下面的例子。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelTutorial
{
    public partial class Form1 : Form
    {
        List<TextBox> textToExcel = new List<TextBox>();
        List<string> s_excelFiles = new List<string> { "Workbook1.xlsx", "Workbook2.xlsx", "Workbook3.xlsx", "Workbook4.xlsx", "Workbook5.xlsx" };
        List<Excel.Application> l_excelApplication = new List<Excel.Application>();
        List<Excel.Workbook> l_excelWorkbook = new List<Excel.Workbook>();
        List<Excel.Worksheet> l_excelWorksheet = new List<Excel.Worksheet>();
        bool fileOpened = false;

        public Form1()
        {
            InitializeComponent();
            textToExcel.Add(textBox1); textToExcel.Add(textBox2); textToExcel.Add(textBox3);
            textToExcel.Add(textBox4); textToExcel.Add(textBox5);

        }

        private void btn_Unopened_Click(object sender, EventArgs e)
        {

            string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

            if (fileOpened != true)
            {

                foreach (var item in s_excelFiles)
                {
                    Excel.Application newExcelApp = new Excel.Application();
                    Excel.Workbook workbook = newExcelApp.Workbooks.Open(path + "\\" + item
                                                 , ReadOnly: false, Editable: true);
                    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.ActiveSheet;
                    newExcelApp.Visible = true;
                    ((Excel.Range)worksheet.Cells[1, 1]).Value = textToExcel[s_excelFiles.IndexOf(item)].Text;
                    l_excelApplication.Add(newExcelApp);
                    l_excelWorkbook.Add(workbook);
                    l_excelWorksheet.Add(worksheet);      
                }
                fileOpened = true;
            }
            else
            {
                foreach (var item in l_excelWorksheet)
                {
                    ((Excel.Range)item.Cells[1, 1]).Value = textToExcel[l_excelWorksheet.IndexOf(item)].Text;

                }
            }
        }

        private void btn_Opened_Click(object sender, EventArgs e)
        {

        }
    }
}

但是,有可能用户在程序运行时已经打开了工作簿。显然,Excel将消息发送给用户,要求重新打开工作簿。这可能会导致数据丢失。 当然,我可以在访问之前检查文件是否已打开,如何使用以下代码对其进行描述here

try
{
    stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}

然后只是要求用户关闭该文件。但我相信还有另一种方法可以做得更好。

所以,我的问题是:如何访问用户已经打开过的文件?

ExcelTutorial解决方案可用here

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码来访问当前活动工作簿:

   //Gets Excel and gets Activeworkbook and worksheet
   Excel.Application oXL;
   Excel.Workbook oWB;
   Excel.Worksheet oSheet;
   oXL = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); 
   oXL.Visible = true;
   oWB = (Excel.Workbook)oXL.ActiveWorkbook; 

   docProps = oWB.CustomDocumentProperties

或许是更好的解决方案:

using Excel = Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;

// Get the currect application instance
Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;

// Get active workbook
Excel.Workbook wbook = xlapp.ActiveWorkbook;

此处有更多信息:Get the current Workbook Object in C#

相关问题