Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I wrote one datatable method.
And I want to create a folder named Notes in the C folder and throw the datatable function excel into it.
datatable how do I assign it into excel.


////I want to create C Folder --> CreateExcelDirectory(NOTES)-->Notes include ExcelTable(ExcelTable name is InformationTable)


How to print datatable excel.

What I have tried:

public DataTable ExcelTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("NameSurname", typeof(string));<
 ..my code.

return dataTable;

}

 Excel.Application ExcelApp = new Excel.Application();
 ExcelApp.CreateDirectory("C:\NOTES\InformationTable");
Posted
Updated 15-Dec-19 4:03am
v2
Comments
OriginalGriff 15-Dec-19 3:55am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. And I have no idea what you mean by "assign it into excel" for example, or what help you actually need.

Use the "Improve question" widget to edit your question and provide better information.
EricERankin 26-Mar-20 13:48pm    
Your question is a bit ambiguous, nevertheless, here is how you can export datatable into excel file:
--------
string directory = @"C:\NOTES";

if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);

var dataTable = ExcelTable();

var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Sheet1");
worksheet.InsertDataTable(dataTable, new InsertDataTableOptions() { ColumnHeaders = true });

var table = worksheet.Tables.Add("InformationTable", worksheet.GetUsedCellRange(true), true);
table.BuiltInStyle = BuiltInTableStyleName.TableStyleMedium2;

workbook.Save(Path.Combine(directory, "InformationTable.xlsx"));
--------

If you need to print an excel file from c# then just add this:

--------
workbook.Print("Your printer's name or use 'null' for default printer.");
--------

According to ApplicationClass Class (Microsoft.Office.Interop.Excel) | Microsoft Docs[^], the Application class does not contain a CreateDirectory method. You need to use the Save or SaveAs method from the WorkBook class.

You should also not create folders in the root of the C: drive; there are much better places to save your data - see Where should I store my data?[^].
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900