How do I format my export to excel workbook in microsoft.office.interop.excel?

前端 未结 4 752
醉梦人生
醉梦人生 2021-01-28 10:39

I have this export function that allows me to export 2 grid views into 2 separated worksheets in one excel.

But my problems are:

  1. How can I have a usual

4条回答
  •  梦谈多话
    2021-01-28 11:24

    you haven't added worksheet in Workbook

    try below code

    worksheet = (Excel._Worksheet)oXL.Worksheets.Add();

    worksheet.Name = SheetName;

    for more info check http://dotnetmentors.com/c-sharp/how-to-export-sql-data-to-excel-using-microsoft-office-interop.aspx

    set your headers using rangs

      string[] colNames = new string[gv.Columns.Count];
    
      int col = 0;
    
      foreach(gvvolumns dc in GridView.Columns)
        colNames[col++] = dc.ColumnName;
    
      char lastColumn = (char)(65 + dtProducts.Columns.Count - 1);
    
      oSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
      oSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
      oSheet.get_Range("A1", lastColumn + "1").VerticalAlignment 
            = Excel.XlVAlign.xlVAlignCenter;
    

提交回复
热议问题