C# .NET - export to excel in c#

Asked By shrikanth A
04-Feb-12 02:24 AM
i exported ma contents excel sheet but after that if i tried to edit using ms exel while saving its prompting like 

-->"may contain features that are not compatible with text tab delimited in excel "

 how can i solve this problem 
 plz help thankzz in adv..
  dipa ahuja replied to shrikanth A
04-Feb-12 03:02 AM
Add the reference in your project. Right click on the project -  > add reference
 
Now from the COM tab add two references
1.    Microsoft Office 12.0 Object Library
2.    Microsoft Excel 12.0 Object Library
 
  using Excel = Microsoft.Office.Interop.Excel;
 private void btnExport_Click(object sender, EventArgs e)
 {    
  // creating Excel Application
   Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
  // creating new WorkBook within Excel application
   Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
  // creating new Excelsheet in workbook
   Excel._Worksheet worksheet = null;
  // see the excel sheet behind the program
   app.Visible = false;
  // get the reference of first sheet. By default its name is Sheet1.
   // store its reference to worksheet
   worksheet = workbook.Sheets["Sheet1"];
   worksheet = workbook.ActiveSheet;
  // changing the name of active sheet
   worksheet.Name = "Exported from gridview";
 
  // storing header part in Excel
   for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
   {
   worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
   }
  // storing Each row and column value to excel sheet
   for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
   {
   for (int j = 0; j < dataGridView1.Columns.Count; j++)
   {
     worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
   }
   }
  // save the application
   workbook.SaveAs("d:\\output.xls", Type.Missing, Type.Missing, Type.Missing,
  Type.Missing,
  Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
  Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  // Exit from the application
   app.Quit();
   MessageBox.Show("Excel file created , you can find the file d:\\output.xls");
 }
 
 
Create New Account
help
EXCEL SHEETS Excel Can you reinstate deleted sheets in excel, if so how please. thanks Excel Worksheet Discussions Microsoft Excel (1) Worksheet (1) Workbook (1) Daisy (1) Closeit (1) If you haven't saved the
Non synchronisation in split screens in Microsoft Excel Excel Lotus 123 has the ability to split a worksheet in two, similar to Microsoft Excel, but in Lotus 123 you have the ability to scroll down one side whilst the other side remains stationary. Can you do that in Microsoft Excel and if so how? Excel Miscellaneous Discussions Microsoft Excel (1) Worksheet (1) Workbook (1) VB (1) Normek (1) Lotus (1
Opening and retrieving information from numerous excel sheets Excel I am currently trying to do work with numerous different excel sheets. I am looking for the user to open a blank Excel worksheet and click a button. This button will make a dialogue box appear and ask a cell from each sheet opened (for example cell A1). How would I do this? Excel Programming Discussions SQL Server 2000 (1) Office 2003 (1) Office (1) Excel (1) Worksheet (1) VbMaximizedFocus (1) BZeyger (1) Rogers (1) Here's something to get you
How can I make my data show as flashing in Excel? Excel Excel Miscellaneous Discussions Microsoft Excel (1) Excel (1) Application.OnTime (1) Worksheet (1) Workbook (1) Macro (1) VBA (1) Font.ColorIndex (1 if you insist, see Chip Pearson's site for VBA code. http: / / www.cpearson.com / excel / BlinkingText.aspx Gord Dibben MS Excel MVP This was great - thank you. My boss loved it. Now I have another question
No colours on excel sheets Excel Excel 2003 No colours show up if i select a colour for figures, letters, frames etc. When printing all colours appear correctly. Which button I pressed by mistake? Excel Crashes GPFs Discussions Excel (1) Windows (1) If this is a problem with all Excel files with coloured cells, the Windows OS high contrast setting may be turned on. There MSKB article: OFF: Changes to Fill Color and Fill Pattern Are Not Displayed http: / / support.microsoft.com / default.aspx?scid = kb;en-ca;320531 - - Gary''s Student - gsnu200715 keywords: No, colours