3

Possible Duplicate:
How to export DataTable to Excel in C#

I have TemplateExcel included in my project, and I want to Export my datatable to Excel, I want to copy and save this template with data everywhere I want to save it, how can I do it??

Community
  • 1
  • 1
Gëzim Shabani
  • 41
  • 1
  • 1
  • 3

2 Answers2

7

You can do it through Excel Interop like this:

using System;
using System.Data;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main()
        {
            Excel.Application application = new Excel.Application();
            Excel.Workbook workbook = application.Workbooks.Add();
            Excel.Worksheet worksheet = workbook.Sheets[1];

            DataTable dataTable = new DataTable();
            DataColumn column = new DataColumn("My Datacolumn");

            dataTable.Columns.Add(column);
            dataTable.Rows.Add(new object[] {"Foobar"});

            var columns = dataTable.Columns.Count;
            var rows = dataTable.Rows.Count;

            Excel.Range range = worksheet.Range["A1", String.Format("{0}{1}", GetExcelColumnName(columns), rows)];

            object[,] data = new object[rows,columns];

            for (int rowNumber = 0; rowNumber < rows; rowNumber++)
            {
                for (int columnNumber = 0; columnNumber < columns; columnNumber++)
                {
                    data[rowNumber, columnNumber] = dataTable.Rows[rowNumber][columnNumber].ToString();
                }
            }

            range.Value = data;

            workbook.SaveAs(@"C:\test\whatever123.xlsx");
            workbook.Close();

            Marshal.ReleaseComObject(application);
        }

        private static string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }
    }
}

All I am doing here is creating a System.Data.DataTable object, filling it with some data and then exporting it to Excel. This needs cleaned up, error handling added, refactored etc but the basis is there.

Credit to Graham for the GetExcelColumnName method.

Community
  • 1
  • 1
JMK
  • 27,273
  • 52
  • 163
  • 280
0

1 You can use this article - based on RenderControl , but you work on your binded Grid

Link : http://www.codeproject.com/Tips/344604/Export-to-EXCEL-from-Datatable-in-Csharp-Net

2 You can base you developement on Table

Link : http://www.codeproject.com/Tips/406704/Export-DataTable-to-Excel-with-Formatting-in-Cshar

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • hmm..workbook.SaveAs(@"C:\test\whatever123.xlsx"); the method SaveAs can't have only one argument :/ – Gëzim Shabani Sep 24 '12 at 09:19
  • thanks guys, the problem is sloved :) – Gëzim Shabani Sep 24 '12 at 17:11
  • Thank you Aghilas, I have another Question if you can help me, I'm getting path this way System.Windows.Forms.Application.StartupPath.Replace(@"bin\Debug", "\\") + "something\\myFile.xls", when I install program the real path is Program Files (x86), but the way how i have call path returns Program Files without "(x86)" and i get error "Path not found" have you any idea how to get right path? – Gëzim Shabani Sep 24 '12 at 17:28