2

I have a code like this,

string ConnectionString = ConfigurationManager.ConnectionStrings["ora"].ConnectionString;

OracleConnection conn = new OracleConnection(ConnectionString);

string sql = @"SELECT DISTINCT(B.MUS_K_ISIM), A.HESAP_NO
  FROM S_TAKIP_MUSTERI A, S_TEKLIF B
  WHERE A.HESAP_NO = B.HESAP_NO
  ORDER BY B.MUS_K_ISIM";

conn.Open();

OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = System.Data.CommandType.Text;

OracleDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

How can i export coming some information from DataReader to Excel? What i want, When i click the button, Export the Excel from DataReader.

Chris Missal
  • 5,987
  • 3
  • 28
  • 46
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    You've got two basic options: use Excel automation to generate the file, or use a library e.g. http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c. If you're running this on a web server it has to be a library, you can't automate Excel. I don't know of any that accept a DataSet as an input, though, but it's a common-enough problem. – Rup Dec 17 '10 at 12:48
  • But there is no example with DataReader..! – Soner Gönül Dec 17 '10 at 12:55
  • No, sorry, I don't have code for that. Basic idea would be 1. Make new workbook, new worksheet. 2. GetSchemaTable() and use this to generate column headings and set data type. 3. Iterate through each row, adding each columm's value to a new cell on the sheet 4. Save, close, clean-up. I expect there's enough documentation to figure it all out. – Rup Dec 17 '10 at 13:05

1 Answers1

0

You could use Microsoft.Office.Interop.Excel to create an instance of excel and then go through and populate the cells in the spreadsheet with the data.

Tony Abrams
  • 4,505
  • 3
  • 25
  • 32
  • I've already got a function that does this with a dataset. So I could whip up a modified function that uses a datareader. It might take a little bit because I'm at work and will be in the middle of other things too. – Tony Abrams Dec 17 '10 at 13:31