Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!
When I export data from datagridview to excel in c# i have a mistake!
The column calculation (it's not in database) I put at column last in datagridview. I can see it in interface, but when I used command export data to excel then the column calculation at the column frist. Can you help set it at column last? Plese .
This is my code to use export to excel.

C#
static public bool exportDataToExcel(string caption, DataGridView grv)
        {
          
            bool result = false;

            Excel.ApplicationClass xlApp;
            Excel.Worksheet xlSheet;
            Excel.Workbook xlBook;
          
            object missValue = System.Reflection.Missing.Value;
    
            xlApp = new Excel.ApplicationClass();
            xlBook = xlApp.Workbooks.Add(missValue);

             xlSheet = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);
          
            xlApp.Visible = false;
            int socot = grv.Columns.GetColumnCount(DataGridViewElementStates.Displayed);
            int sohang = grv.Rows.Count;//dt.Rows.Count;//
            int i,j;

            SaveFileDialog f = new SaveFileDialog();
            f.Filter = "Excel file (*.xls)|*.xls";
            if (f.ShowDialog() == DialogResult.OK)
            {

              
                xlSheet.get_Range("B1", Convert.ToChar(socot + 65) + "1").Merge(false);
                Excel.Range caption = xlSheet.get_Range("B2", Convert.ToChar(socot + 65) + "1");
                caption.Select();
                caption.FormulaR1C1 = caption;
				
                caption.HorizontalAlignment = Excel.Constants.xlCenter;
                caption.Font.Bold = true;
                caption.VerticalAlignment = Excel.Constants.xlCenter;
                caption.Font.Size = 16;
                caption.Font.Name = "Arial";
                caption.Font.ColorIndex = 3;
               
				
                caption.Interior.ColorIndex = 6;
                caption.RowHeight = 25;
     
                Excel.Range header = xlSheet.get_Range("B2", Convert.ToChar(socot + 65) + "2");
                header.Select();
                header.HorizontalAlignment = Excel.Constants.xlCenter;
                header.Font.Bold = true;
                header.Font.Size = 12;
                header.Font.ColorIndex=2;
                header.Font.Name = "Arial";
                header.Interior.ColorIndex = 25;
               
                for (i = 0; i<socot ; i++)
                {
                    xlSheet.Cells[2, i + 2] = grv.Columns[i].HeaderText;
             }
                for (i = 0; i < sohang; i++)
                    for (j = 0; j < socot; j++)
                    {
                       
                        xlSheet.Cells[i + 3, j + 2] = grv.Rows[i].Cells[j].Value.ToString();//dt.Rows[i][j].ToString();//;
                        ((Excel.Range)xlSheet.Cells[i + 3, j + 2]).Borders.ColorIndex = 25;
                        ((Excel.Range)xlSheet.Cells[i + 3, j+2]).VerticalAlignment = Excel.Constants.xlCenter;
                        if (j == 0 || j == 1 || j == 7)
                            ((Excel.Range)xlSheet.Cells[i + 3, j + 2]).HorizontalAlignment = Excel.Constants.xlCenter;
                        if (j == 3)
                        {
                         ((Excel.Range)xlSheet.Cells[i + 3, j + 2]).HorizontalAlignment = Excel.Constants.xlLeft;
                           
                        }
    ((Excel.Range)xlSheet.Cells[i + 3, 7]).WrapText = true;
 
                    }
                
                for (i = 0; i < socot; i++)
                {
                    ((Excel.Range)xlSheet.Cells[1, i + 1]).EntireColumn.AutoFit();
              
                }
              
                //save file
                xlBook.SaveAs(f.FileName, Excel.XlFileFormat.xlWorkbookNormal, missValue, missValue, missValue, missValue, Excel.XlSaveAsAccessMode.xlExclusive, missValue, missValue, missValue, missValue, missValue);
                xlBook.Close(true, missValue, missValue);
                xlApp.Quit();
                
               
                releaseObject(xlSheet);
                releaseObject(xlBook);
                releaseObject(xlApp);
                result = true;
            }
            return result;
        }
Posted
Updated 18-May-11 0:24am
v2

I have done yet :D
Thanks for helping me
 
Share this answer
 
I am uday satardekar from ghotgewadi dodamarg.
please tell me how in details how to save grid columns in excel
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900