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

前端 未结 4 679
醉梦人生
醉梦人生 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:12

    Modify the path of the excel file to save to a virtual path as follows

    workbook.SaveAs(@"C:\Users\test\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".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);
    

    change this to

    workbook.SaveAs(@"~/ExcelFiles/Filename.xlsx" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".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);
    

    Try the following code for displaying save as dialog

    String FileName = "FileName.xlsx";
    String FilePath = "~/ExcelFiles/FileName.xlsx"; 
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.ClearContent();
    response.Clear();
    response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
    response.TransmitFile(FilePath);
    response.Flush();
    response.End();
    
    0 讨论(0)
  • 2021-01-28 11:18

    Try this:

    Excel.Worksheet worksheet =(Excel.Worksheet)workbook.Worksheets["Sheet" + sheetid];
    
    0 讨论(0)
  • 2021-01-28 11:19

    as specified by Laxmikant modify the code of your method "ExportToExcel" as follows.

    public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook, GridView gridview, string SheetName, int sheetid)
    {
    
    // see the excel sheet behind the program
    app.Visible = true;
    
    worksheet = (Excel.Worksheet)workbook.Worksheets.Add();
    
    // changing the name of active sheet
    worksheet.Name = SheetName;
    
    // storing header part in Excel
    for (int i = 1; i < gridview.Columns.Count + 1; i++)
    {
        worksheet.Cells[1, i] = gridview.Columns[i - 1].HeaderText;
    }
    
    // storing Each row and column value to excel sheet
    for (int i = 0; i < gridview.Rows.Count - 1; i++)
    {
        for (int j = 0; j < gridview.Columns.Count; j++)
        {
            worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Text.ToString();
        }
    }
    

    I removed these two lines of code and now there is no need of the parameter "Sheetid"

    Excel.Worksheet worksheet =(Excel.Worksheet)workbook.Worksheets["Sheet" + sheetid];
    worksheet = workbook.ActiveSheet;
    

    Hope this will solve your issue

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题