gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:23 25 4
gpt4 key购买 nike

请帮我解决这些问题!

在我的新项目中,我需要访问一些 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 !

最佳答案

例如,您可以尝试使用此代码来访问当前事件的工作簿:

   //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#

关于c# 如何访问打开的 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26547880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com