Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sorry, we couldn't find E:\Tst.xlsx. Is it possible it was moved, renamed or deleted?

C#
DataTable dt = new DataTable();

//Add Datacolumn
DataColumn workCol = dt.Columns.Add("FirstName", typeof(String));           
dt.Columns.Add("Blog", typeof(String));
dt.Columns.Add("City", typeof(String));
dt.Columns.Add("Country", typeof(String));

//Add in the datarow
DataRow newRow = dt.NewRow();

newRow["firstname"] = "Faisal";            
newRow["Blog"] = "http://google.com/";
newRow["city"] = "Mysore";
newRow["country"] = "India";

dt.Rows.Add(newRow);

DataSet ds = new DataSet();
ds.Tables.Add(dt);



ExportDataSetToExcel(ds, "E:\\Tst.xlsx");


private void ExportDataSetToExcel(DataSet ds, string _filename)
{
    //Creae an Excel application instance
    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
    
    //Create an Excel workbook instance and open it from the predefined location
    //ERROR
    Microsoft.Office.Interop.Excel.Workbook excelWorkBook = excelApp.Workbooks.Open(_filename);
    
    
    foreach (DataTable table in ds.Tables)
    {
        //Add a new worksheet to workbook with the Datatable name
        Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
        excelWorkSheet.Name = table.TableName;
        
        for (int i = 1; i < table.Columns.Count + 1; i++)
        {
            excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
        }
        
        for (int j = 0; j < table.Rows.Count; j++)
        {
            for (int k = 0; k < table.Columns.Count; k++)
            {
             excelWorkSheet.Cells[j+2, k+1] = table.Rows[j].ItemArray[k].ToString();
            }
        }
    }

    excelWorkBook.Save();
    excelWorkBook.Close();
    excelApp.Quit();
}
Posted
Updated 26-Apr-16 1:39am
v3
Comments
Patrice T 26-Apr-16 6:35am    
And you have a question ?

Create a file E:\\Tst.xlsx first.
Because you are opening it in code, not creating so the error.
 
Share this answer
 
Comments
[no name] 26-Apr-16 7:16am    
How do i create excel file with c# code ?
Asmita Bhurke 28-Apr-16 2:41am    
replace line

excelApp.Workbooks.Open(_filename)

with

excelApp.Workbooks.Add(_filename)


https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.add.aspx
C#
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
               Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
               Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
               object misValue = System.Reflection.Missing.Value;

               xlWorkBook = xlApp.Workbooks.Add(misValue);
               xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
               xlWorkSheet.Cells[1, 1] = "Sheet 1 content";

               xlWorkBook.SaveAs(_path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
               xlWorkBook.Close(true, misValue, misValue);
               xlApp.Quit();

               releaseObject(xlWorkSheet);
               releaseObject(xlWorkBook);
               releaseObject(xlApp);



C#
private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        }
 
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