I am using C# and Visual studio 2010. I am using a ASP.NET web application. I am trying to pass the data from Oracle ( data available in a datatable ) to multiple sheets within the same excel. The user should get a prompt to save the file. I have tried the below way, but it is affecting the performance. Is there any different way which can be achieved. I cannot use a third party tool. There are no macros in the excel and is a simple data transfer to excel from datatable. No formatting required in excel. The datatable is huge.
Excel.Application oXL = new Excel.Application();
Excel.Workbook oWB = oXL.Workbooks.Add(missing);
Excel.Worksheet oSheet = oWB.ActiveSheet as Excel.Worksheet;
using (OracleConnection con = new OracleConnection(oradb))
{
con.Open();
test2(oSheet, "select * from table1", "Names", con);
Excel.Worksheet oSheet2 = oWB.Sheets.Add(missing, missing, 1, missing) as Excel.Worksheet;
test2(oSheet2, "select * from table2", "Address", con);
Excel.Worksheet oSheet3 = oWB.Sheets.Add(missing, missing, 2, missing) as Excel.Worksheet;
test2(oSheet3, "select * from table3", "Phones", con);
}
}
public static void test2(Excel.Worksheet oSheet, string sql, string name, OracleConnection con)
{
OracleDataAdapter da = new OracleDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt == null || dt.Columns.Count == 0)
{
}
else
{
oSheet.Name = name;
for (var i = 0; i < dt.Columns.Count; i++)
{
oSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
}
for (var i = 0; i < dt.Rows.Count; i++)
{
for (var j = 0; j < dt.Columns.Count; j++)
{
oSheet.Cells[i + 2, j + 1] = dt.Rows[i][j];
}
}
}
}
}
How to pass data to multiple sheets within same excel and prompt user to save the file ?