Click here to Skip to main content
15,887,927 members

Comments by prahalad.gaggar (Top 13 by date)

prahalad.gaggar 12-Dec-12 8:19am View    
That's using PHP. Tried it!!! Not useful!!
prahalad.gaggar 6-Dec-12 9:08am View    
@nyab31 please check my answer!!!
i think now i am generating a valid Excel(.xlsx) file!!!
prahalad.gaggar 6-Dec-12 9:06am View    
your code cannot be deployed deployed on cloud, because office automation is illegal on asp.net, for that u have to use share-point!!!
which is very tedious!!!
prahalad.gaggar 6-Dec-12 9:00am View    
Doesn't it takes time if you have a data-table with more than 500 records!!!
prahalad.gaggar 6-Dec-12 8:51am View    
Very Untidy code, also takes a lot time to generate the excel file!!!
10 - 15 secs just to enter 800 records!!!
I edited the code:

protected void to_excel(object sender, EventArgs e)
{
string filepath = Path.Combine(Server.MapPath("~/Files"), fileupload.FileName);
fileupload.SaveAs(filepath);
string fname = fileupload.PostedFile.FileName;
DataTable dt = (DataTable)ReadToEnd(filepath);
string sFilename = fname.Substring(0, fname.IndexOf("."));
sFilename = sFilename + ".xlsx";
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet objSheet = new Microsoft.Office.Interop.Excel.Worksheet();
Microsoft.Office.Interop.Excel.Workbook objWorkBook = null;
app.Visible = true;
objSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
objSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
objSheet.Name = "Sheet1";
int col = 1;
int row = 1;
foreach (DataColumn column in dt.Columns)
{
objSheet.Cells[row, col] = column.ColumnName.ToString();
col++;
}
col = 1;
row = 2;
foreach (DataRow rw in dt.Rows)
{
foreach (DataColumn cl in dt.Columns)
{
if (rw[cl.ColumnName] != DBNull.Value)
objSheet.Cells[row, col] = rw[cl.ColumnName].ToString();
col++;
}
row++;
col = 1;
}
workbook.SaveAs(sFilename, Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}

Also i don't know how to auto save the result!!!
Total Waste, this code won't work in real world!!!

Better to use my solution. (I am not saying this because i found the solution, but after comparing your result with mine i came to this conclusion)

No hard Feelings bro!!!
And I appreciate your help!!!
Keep Smiling :)