Wednesday, 12 September 2012
C# - Excel - How to create simple Excel file programatically
Watch this example on YouTube:
1. First add Excel reference (Microsoft Excel 14.0 Object Library) from COM tab to your project
Add the following
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
Add button to the form and the following on click
private void button1_Click(object sender, EventArgs e)
{
string fileTest = "C:\\Temp\\ExcelTest\\test.xlsx";
if (File.Exists(fileTest))
{
File.Delete(fileTest);
}
Excel.Application oApp;
Excel.Worksheet oSheet;
Excel.Workbook oBook;
oApp = new Excel.Application();
oBook = oApp.Workbooks.Add();
oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
oSheet.Cells[1, 1] = "some value";
oBook.SaveAs(fileTest);
oBook.Close();
oApp.Quit();
}
That's it, file has been created. Watch this example on YouTube:
Subscribe to:
Post Comments (Atom)
wow THANK you alot...
ReplyDeleteJust for others who had the same error with me and could not see the video clearly--
I changed the
FROM: string fileTest = "C:\\Temp\\ExcelTest\\test.xlsx";
TO:string fileTest = "C:\\more.xlsx";
Then the whole code worked
Schreibt stat
ReplyDeletestring fileTest = "C:\\Temp\\ExcelTest\\test.xlsx";
das
string fileTest = @"C:\Temp\ExcelTest\test.xlsx";
I ran into an error on
ReplyDelete"oBook = oApp.Workbooks.Add();"
"Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."
Any idea how to solve it? Cheers,
Have you sorted the error? I get the same one
Deleteyou must have excel installed on your computer. if this is not an option than use OpenXML - example here https://www.youtube.com/watch?v=YAXzQ3sXxxE
DeleteI have used Aspose.Cells for .NET Library to create an excel file and it has generated very good results and they offer many more code sample to be used in application. Try it:
ReplyDeletehttp://www.aspose.com/.net/excel-component.aspx
Finally, I got the code tht interact with an excel
ReplyDeleteThis Error is shown
ReplyDeleteMicrosoft Excel cannot access the file 'C:\Temp\ExcelTest\3C6CEB00'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
hello
ReplyDeletecan you tell me how to create .aspx file
please make a video on it
I am using Visual Studio 2017 want to create a new excel file and wish to add spreadsheets to it.
ReplyDeleteGetting an error at
oSheet = (Excel.Worksheet)oBook.Worksheets.get_item(1);
I have been looking at C# to Excel code for weeks. Its all has so many extra steps which are unnecessary. This code works amazingly. Great work and thanks for sharing!
ReplyDelete