Aspose. Cells component exports excel file

  • 2021-11-02 00:25:59
  • OfStack

This article example for everyone to share Aspose. Cells export excel file method, for your reference, the specific content is as follows


/// <summary>
    ///  Export data to local 
    /// </summary>
    /// <param name="dt"> Data to export </param>
    /// <param name="tableName"> Export name </param>
    /// <param name="tableTitle"> Table row name format "account number, password" </param>
    /// <param name="response"> Request </param>
    public static void OutFileToDisk(DataTable dt, string tableName, string tableTitle, HttpResponse response)
    {
      Workbook workbook = new Workbook(); // Workbook  
      Worksheet sheet = workbook.Worksheets[0]; // Worksheet  
      Cells cells = sheet.Cells;// Cell  

      // Style the title    
      Style styleTitle = workbook.Styles[workbook.Styles.Add()];// New Style  
      styleTitle.HorizontalAlignment = TextAlignmentType.Center;// Text centered  
      styleTitle.Font.Name = " Song Style ";// Text font  
      styleTitle.Font.Size = 18;// Text size  
      styleTitle.Font.IsBold = true;// Bold font  

      // Style 2 
      Style style2 = workbook.Styles[workbook.Styles.Add()];// New Style  
      style2.HorizontalAlignment = TextAlignmentType.Center;// Text centered  
      style2.Font.Name = " Song Style ";// Text font  
      style2.Font.Size = 14;// Text size  
      style2.Font.IsBold = true;// Bold font  
      style2.IsTextWrapped = true;// Wrap cell contents  
      style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
      style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
      style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
      style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

      // Style 3 
      Style style3 = workbook.Styles[workbook.Styles.Add()];// New Style  
      style3.HorizontalAlignment = TextAlignmentType.Center;// Text centered  
      style3.Font.Name = " Song Style ";// Text font  
      style3.Font.Size = 12;// Text size  
      style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
      style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
      style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
      style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

      int Colnum = dt.Columns.Count;// Number of table columns  
      int Rownum = dt.Rows.Count;// Number of table rows  

      // Generate rows 1  Header row   
      cells.Merge(0, 0, 1, Colnum);// Merge cells  
      cells[0, 0].PutValue(tableName);// Fill in the contents  
      cells[0, 0].SetStyle(styleTitle);
      cells.SetRowHeight(0, 38);

      // Generate rows 2  Column name row  
      string[] Tile = tableTitle.Split(',');
      for (int i = 0; i < Colnum; i++)
      {
        cells[1, i].PutValue(Tile[i]);
        cells[1, i].SetStyle(style2);
        cells.SetRowHeight(1, 25);
      }

      // Generate data rows  
      for (int i = 0; i < Rownum; i++)
      {
        for (int k = 0; k < Colnum; k++)
        {
          cells[2 + i, k].PutValue(dt.Rows[i][k].ToString());
          cells[2 + i, k].SetStyle(style3);
        }
        cells.SetRowHeight(2 + i, 24);
      }
      workbook.Save(response, HttpUtility.UrlEncode(tableName, System.Text.Encoding.UTF8) + ".xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));
    }

Call


string tableTitle = " Account number, password ";
    ExcelHelp.OutFileToDisk(dt, " Account information ", tableTitle , HttpContext.Current.Response);

Foreground page


window.open(" Method ", "_blank");// Click to download 

Aspose. Cells. dll Download Address


Related articles: