简体   繁体   English

如何将单个工作表从一个.XLS文件复制到另一个文件?

[英]How can I copy a single worksheet from one .XLS file to another?

I'm struggling to copy an entire worksheet from an existing .XLS file into a new Excel file I'm creating within my application. 我正在努力将整个工作表从现有的.XLS文件复制到我在应用程序中创建的新Excel文件中。 This is my code so far: 到目前为止,这是我的代码:

Microsoft.Office.Interop.Excel.Workbook tempworkbook = workbooks.Open(
           Directory.GetCurrentDirectory() + "\\Template.xlsx", //FileName
           0, //UpdateLinks
           true, //ReadOnly
           5, //Format
           Type.Missing, //Password
           Type.Missing, //WriteResPassword
           true,//IgnoreReadOnlyRecommended
           Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, //Origin
           "\t", //Delimiter
           false, //Editable
           false, //Notify
           0, //Converter
           false, //AddToMRU
           1, //Local
           0 //CorruptLoad
           );
        Microsoft.Office.Interop.Excel.Worksheet tmp1 = (Microsoft.Office.Interop.Excel.Worksheet)tempworkbook.Sheets["GSM Data"];
        string test = tmp1.get_Range("B2", "B2").Value2.ToString(); //test to see if sheet can be accessed - value should be "Database";
        tmp1.Copy(Type.Missing, wsGSMData); //copy worksheet into wsGSMData (which is the sheet in the new XLS file) 

However the error I'm getting on the last line is: 但是我在最后一行遇到的错误是:

The server threw an exception. 服务器引发异常。 (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) (来自HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))

Essentially, the sheet I've extracted as tmp1 should go into the sheet called wsGSMData , which is a sheet (one of 7 sheets in total) that i've created within the new .XLS workbook (called subsetworkbook): 本质上,我作为tmp1提取的工作表应该进入名为wsGSMData的工作表,这是我在新的.XLS工作簿(称为subsetworkbook)中创建的工作表(总共7个工作表之一):

 Microsoft.Office.Interop.Excel.Worksheet wsGSMData = (Microsoft.Office.Interop.Excel.Worksheet)subsetworkbook.Sheets["Sheet7"];

What am I doing wrong? 我究竟做错了什么?

Probably you've got an issue with Access Mode for your Excel file, which causes (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) . 可能您的Excel文件的访问模式存在问题,这会导致(HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))

Try to check settings in Excel menu: Review > Share Workbook > Allow changes by more than one user... - you should try to deselect this check box. 尝试检查Excel菜单中的设置: 复查>共享工作簿>允许多个用户更改...-您应尝试取消选中此复选框。

Then go back to your code and check any lines, where you could possibly change this settings, especially SaveAs function. 然后返回到您的代码并检查所有行,您可以在其中更改此设置,尤其是SaveAs函数。 If you see xlShared , replace it with xlExclusive (see example below). 如果看到xlShared ,请将其替换为xlExclusive (请参见下面的示例)。

I've had the same problem while trying to add charts to file. 尝试将图表添加到文件时遇到了相同的问题。 No googled solution can help in my case, so I've got to figure out, what's going wrong, by myself. Google解决方案无法解决我的问题,因此我必须自己找出问题所在。 After trying to add charts into a fresh new file by different methods, I realise that aforementioned exception in this line: 在尝试通过不同方法将图表添加到新的新文件中之后,我意识到此行中的上述异常:

Microsoft::Office::Interop::Excel::ChartObject ^ExcelChartObj = ExcelChartObjs->Add(100, 100, 300, 250); //create chart object (left, top, width, height)

was actually caused by the fact, that original file had "Allow changes by more than one user" checkbox selected from the start, and moreover, my own code also re-saved it as xlShared - twice! 实际上是由以下事实引起的:原始文件从一开始就选中了“允许多个用户更改”复选框,此外,我自己的代码还将其重新保存为xlShared-两次! (Nope, double saving is not an error, it is how it should be in my case, cause my program solves a huge task, and I need to input data into the same file in two different steps during it) (是的,双重保存不是错误,在我的情况下是这样,因为我的程序解决了一个繁重的任务,在此过程中,我需要通过两个不同的步骤将数据输入到同一文件中)

Here's the working code (for C++/CLI Windows Forms), which finally made (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) to disappear in my case: 这是工作代码(适用于C ++ / CLI Windows窗体),最终使我的情况下,HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))消失了:

ExcelApp->ActiveWorkbook->SaveAs(ExcFileName, Microsoft::Office::Interop::Excel::XlFileFormat::xlOpenXMLWorkbook, Type::Missing, Type::Missing,
    Type::Missing, Type::Missing, Microsoft::Office::Interop::Excel::XlSaveAsAccessMode::xlExclusive, Type::Missing, Type::Missing, Type::Missing,
    Type::Missing, Type::Missing); //save Excel book (access mode - xlExclusive, not xlShared!)

I hope it will help someone out there too! 我希望它也会对那里的人有所帮助! :) :)

暂无
暂无

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

相关问题 如何从一个文件复制数据到另一个文件 - How I can copy data from one file in another one 如何将节点从一个xml文件复制到另一个文件? - How can I copy nodes from one xml file into another? C# - 如何将单个 Excel 工作表从一个工作簿复制到另一个工作簿? - C# - How to copy a single Excel worksheet from one workbook to another? c#-(Excel互操作)如何在VSTO中将一个工作表复制到另一工作簿? - c# - (Excel interop)How can I copy one worksheet to another workbook in VSTO? 如何将一个属性从一个类复制到另一个类? - How can I copy one attribute from one class to another? 从另一个文件复制 Excel 工作表 - Copy Excel worksheet from another file 将选定的范围从一个 xls 文件粘贴到另一个指定的工作表和单元格 - “范围 class 的复制方法失败” - Paste selected range from one xls file to another into a designated sheet and cell - “Copy method of Range class failed” 如何将数据绑定从一个控件复制到另一个控件? - How can I copy the databinding from one control to another? 从文件夹中获取 xslx 和 xls 文件并将其复制到另一个文件夹中 - Get xslx and xls file from a folder and copy it in another folder 如何将特定单元格(带有值、颜色、字体)从一个 excel 文件复制到另一个 excel 文件 C#? - How can I copy specific cells(with Value,Color,Font) from one excel file to another excel file C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM