PC Review


Reply
Thread Tools Rate Thread

Create and open Excel spreadsheet from ASP.NET

 
 
John Straumann
Guest
Posts: n/a
 
      10th Nov 2009
Hi all:

I found an earlier thread that showed creating an Excel file from ASP.NET,
pasted below and works great! However I set the code to create 3 columns and
25 data rows, and it creates a CSV file but when I open the file in Excel
the data shows up in one column thus:

A
Col 1;Col 2; Col3;
0;2;3
1;3;4;

etc.

So all the data is showing up in the first column, separated by ;.

Does anyone know how I could modify the code so I can create separate
columns in Excel?

Thanks for any and all input.

John.

public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ";");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(";",
string.Empty) + ";");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment;
filename=" + name + ".csv");
context.Response.End();
}



 
Reply With Quote
 
 
 
 
John Straumann
Guest
Posts: n/a
 
      11th Nov 2009
Thanks, Mark.

JS.

"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> "John Straumann" <(E-Mail Removed)> wrote in message
> news:FD3FD984-FB66-4AD5-9C52-(E-Mail Removed)...
>
>> context.Response.Write(column.ColumnName + ";");
>> context.Response.Write(row[i].ToString().Replace(";", string.Empty) +
>> ";");

>
>> So all the data is showing up in the first column, separated by ;.
>>
>> Does anyone know how I could modify the code so I can create separate
>> columns in Excel?

>
> "CSV" = "comma-separated values", so...
>
> context.Response.Write(column.ColumnName + ",");
>
> context.Response.Write(row[i].ToString().Replace(",", string.Empty) +
> ",");
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net


 
Reply With Quote
 
Mark Stevens
Guest
Posts: n/a
 
      11th Nov 2009
There is a rather good library on Codeplex which can write to Excel
2007 spreadsheets. Check out http://simpleooxml.codeplex.com/.

I've used it and the only problems I've found are

1. Formatting can max out the CPU.
2. It fails to paste bit field correctly (this has been reported as an
issue).

Hope this helps,
Mark
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens (mark at thepcsite fullstop co fullstop uk)

This message is provided "as is".
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Freeze Excel Spreadsheet till template open first then spreadsheet learning_codes@hotmail.com Microsoft Excel Discussion 1 26th Oct 2008 03:56 PM
Create a .xla to open a spreadsheet =?Utf-8?B?RGlhbmU=?= Microsoft Excel Programming 4 3rd Oct 2006 12:59 PM
Can I create an Excel calendar that references Excel spreadsheet? =?Utf-8?B?aW50ZXJuLCBha2E6IHNsYXZlIGxhYm9y?= Microsoft Excel Misc 1 24th Jul 2006 06:31 PM
can i create a shortcut to open a spreadsheet read only? =?Utf-8?B?TWFyaw==?= Microsoft Excel Misc 1 21st Sep 2005 01:13 PM
Re: How to open another Excel spreadsheet to copy data into current spreadsheet ? Ricky Pang Microsoft Excel Programming 0 13th Jul 2003 01:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:03 PM.