使用 C# 写入 Excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19933135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing to Excel using C#
提问by bez91
I have a basic WinForms application and want to be able to write data from this application to an Excel spreadsheet. So far I have the following code:
我有一个基本的 WinForms 应用程序,希望能够将此应用程序中的数据写入 Excel 电子表格。到目前为止,我有以下代码:
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
excelapp.Visible = true;
_Workbook workbook = (_Workbook)(excelapp.Workbooks.Add(Type.Missing));
_Worksheet worksheet = (_Worksheet)workbook.ActiveSheet;
worksheet.Cells[1, 1] = "Name";
worksheet.Cells[1, 2] = "Bid";
worksheet.Cells[2, 1] = txbName.Text;
worksheet.Cells[2, 2] = txbResult.Text;
excelapp.UserControl = true;
Now what I want to do be able to do is write to an Excel file that has already been created, thus appending it. I also want it to do this all behind the scenes so that on a button click the data is written to the spreadsheet and saved without any interaction from the user. Whilst just adding data to the file NOT overwriting it.
现在我想要做的是写入一个已经创建的 Excel 文件,从而附加它。我还希望它在幕后完成这一切,以便在单击按钮时将数据写入电子表格并保存,而无需用户进行任何交互。虽然只是将数据添加到文件中而不是覆盖它。
采纳答案by user2140261
To open an existing workbook change your code from
要打开现有的工作簿,请更改您的代码
_Workbook workbook = (_Workbook)(excelapp.Workbooks.Add(Type.Missing));
To
到
_Workbook workbook = (_Workbook)(excelapp.Workbooks.Open(@"C:\Path\To\Your\WorkBook\ExcelWorkBook.Xlsm"));
Then to save it you would simply call the Save method of the open workbook:
然后要保存它,您只需调用打开的工作簿的 Save 方法:
workbook.Save();
If you don't want the user to see anything that is going on and have it all run in the background you should also change your line of code:
如果您不希望用户看到正在发生的任何事情并让它在后台运行,您还应该更改您的代码行:
excelapp.Visible = true;
To
到
excelapp.Visible = false;
The only part of your question I don't under stand is you statement Whilst just adding data to the file NOT overwriting it.
If you add data to a file and save that file it will overwrite. Did you want to save the new workbook as a different file? Thus having 2 files?
我不明白的问题的唯一部分是你声明Whilst just adding data to the file NOT overwriting it.
如果将数据添加到文件并保存该文件,它将被覆盖。是否要将新工作簿另存为其他文件?因此有2个文件?
NOTE:These do notrequire any third party software beyond the Interop you are already using, these are all built in function to the Interop. This mean you won't have to download anything or add any extra dependencies on your project.
注意:这些并不需要你已经在使用任何第三方软件超越了互操作,这些都是建立在功能上的互操作。这意味着您无需下载任何内容或为您的项目添加任何额外的依赖项。
回答by Joe Ratzer
回答by CheGueVerra
I found this when I was filling pre made excel templates, really nice
我在填写预先制作的 excel 模板时发现了这个,真的很好
Creating Excel spreadsheets .XLS and .XLSX in C#
在 C# 中创建 Excel 电子表格 .XLS 和 .XLSX
Also look at his other examples very cool stuff
也看看他的其他例子非常酷的东西
回答by ZanderAdam
If you are working with Excel 2007 and up, you can give OpenXML a try. I had great success using this to create new and modify existing excel spreadsheets.
如果您使用的是 Excel 2007 及更高版本,则可以尝试使用 OpenXML。我使用它来创建新的和修改现有的 Excel 电子表格取得了巨大的成功。
Download libraries from: http://www.microsoft.com/en-us/download/details.aspx?id=5124
从以下位置下载库:http: //www.microsoft.com/en-us/download/details.aspx?id=5124
There are a lot of useful questions/answers on SO that will help you. Here is a good one:
SO上有很多有用的问题/答案可以帮助您。这是一个很好的:
回答by ???? ???????
You need to ?Add_Ins programming and guest do the same work, and when the user saves a file Excel automatically do what you want as follows:
你需要 ?Add_Ins 编程和来宾做同样的工作,当用户保存文件时 Excel 自动做你想做的如下:
this .Application .WorkbookBeforeSave +=new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
void Application_WorkbookBeforeSave(Excel.Workbook workbook,bool ui,ref bool ok)
{
Worksheet worksheet = (Worksheet)workbook.ActiveSheet;
worksheet.Cells[1, 1] = "Name";
worksheet.Cells[1, 2] = "Bid";
string filepath = @"Path";
DirectoryInfo d = new DirectoryInfo(filepath);
var file =d.GetFiles("*infousers.txt"));
string[] info = File.ReadAllLines(filepath + @"\" + file.Name);
string userName=info[0];
string userId=info[1];
worksheet.Cells[2, 1] = userName.ToString();
worksheet.Cells[2, 2] = userId.ToString();
}