简体   繁体   English

在 C# 中从不同的 Excel 工作表复制数据

[英]Copying data from different Excel worksheets in C#

I am using a Windows Desktop App and I wish to open a new Excel workbook and worksheet and then have the user select an existing Excel file so that the program can copy data onto the new sheet.我正在使用 Windows 桌面应用程序,我希望打开一个新的 Excel 工作簿和工作表,然后让用户选择一个现有的 Excel 文件,以便程序可以将数据复制到新工作表上。 The user should do this twice, since two Excel files are used for reference.用户应执行此操作两次,因为使用了两个 Excel 文件作为参考。

I know that to get the new Excel workbook and worksheet, you'd do the following:我知道要获取新的 Excel 工作簿和工作表,您需要执行以下操作:

Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;

oXL = new Excel.Application();
oXL.Visible = true;

//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
string sheetName = comboBox1.Text;
oSheet.Name = sheetName;

But where I get lost is how to reference the file that the user selects... Would you do...但是我迷路的是如何引用用户选择的文件......你会这样做......

string excelPath = string.Empty;
using (OpenFileDialog excelFile = new OpenFileDialog())
{
    excelFile.InitialDirectory = "c:\\";
    if(excelFile.ShowDialog() == DialogResult.OK)
    {

    }
}

After that how do I reference the data in the selected Excel file to, for example, extract everything from Column A and copy it into the new Excel file?之后我如何引用所选 Excel 文件中的数据,例如,从 A 列中提取所有内容并将其复制到新的 Excel 文件中?

Here's a little function I used in the past to do this through the interop's clipboard functionality:这是我过去通过互操作的剪贴板功能使用的一个小功能:

using Microsoft.Office.Interop.Excel;

public bool XlsCopyPaste(ref Workbook srcWb,
                         string srcSheetName, string srcRange,
                         ref Workbook tgtWb,
                         string tgtSheetName, string tgtRange,
                         XlPasteType pasteType)
{
    try
    {   
        var srcWs = srcWb.Sheets.Cast<Worksheet>().ToList();
        var tgtWs = tgtWb.Sheets.Cast<Worksheet>().ToList();

        var srcSheet = srcWs.FirstOrDefault(s => s.Name == srcSheetName);
        if (srcSheet != null)
        {
            var tgtSheet = tgtWs.FirstOrDefault(s => s.Name == tgtSheetName);
            if (tgtSheet == null)
            {
                tgtSheet = tgtWb.Sheets.Add();
                tgtSheet.Name = tgtSheetName;
            }

            var source = srcSheet.Range(srcRange);
            var target = tgtSheet.Range(tgtRange);

            source.Copy();
            target.PasteSpecial(pasteType, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
        }
    }
    catch (Runtime.InteropServices.COMException ex)
    {
        return false;
    }
}

And you can call it like this:你可以这样称呼它:

var xl = new Application();
var srcWb = (Workbook)xl.Workbooks.Open(xl.GetOpenFilename("Excel Workbooks (*.xls; *.xlsx),*.xls;*.xlsx").ToString());
var tgtWb = (Workbook)xl.Workbooks.Open(xl.GetOpenFilename("Excel Workbooks (*.xls; *.xlsx),*.xls;*.xlsx").ToString());

if (XlsCopyPaste(srcWb, "Sheet1", "B:C", tgtWb, "Sheet2", "A1", XlPasteType.xlPasteAll))
{
    tgtWb.Save();
}

srcWb.Close();
tgtWb.Close();

xl.Quit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM