I need a solution to export a dataset to an excel file without any asp code (HttpResonpsne…) but i did not find a good example to do this…
Best thanks in advance
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’ve created a class that exports a
DataGridVieworDataTableto an Excel file. You can probably change it a bit to make it use yourDataSetinstead (iterating through theDataTablesin it). It also does some basic formatting which you could also extend.To use it, simply call ExcelExport, and specify a filename and whether to open the file automatically or not after exporting. I also could have made them extension methods, but I didn’t. Feel free to.
Note that Excel files can be saved as a glorified XML document and this makes use of that.
EDIT: This used to use a vanilla
StreamWriter, but as pointed out, things would not be escaped correctly in many cases. Now it uses aXmlWriter, which will do the escaping for you.The
ExcelWriterclass wraps anXmlWriter. I haven’t bothered, but you might want to do a bit more error checking to make sure you can’t write cell data before starting a row, and such. The code is below.Then you can export your
DataTableusing the following: