Asp.net & Aspose.cells 匯出

來源:互聯網
上載者:User

標籤:

 1   protected void btnExport_Click(object sender, EventArgs e) 2     { 3         DataTable tbBooks = (DataTable)Session["Books"]; 4         if (tbBooks == null) 5         { 6             return; 7         }13         try14         {15             Workbook newWorkBook = NewExcel();16             Aspose.Cells.Worksheet newSheet = newWorkBook.Worksheets[0];17             Cells newCells = newSheet.Cells;18 19             if (tbBooks != null)20             {21                 for (int i = 0; i < tbBooks.Rows.Count; i++)22                 {25                     for (int j = 0; j < tbBooks.Columns.Count; j++)26                     {28                         newCells[i+1, j].PutValue(tbBooks.Rows[i][j].ToString());29                     }30                 }31             }32             newWorkBook.Save("result.xlsx", Aspose.Cells.FileFormatType.Excel97To2003, Aspose.Cells.SaveType.OpenInExcel, Response);70         }71         catch (Exception ex)72         {73             MessageBox.Show(this, "匯出產品資訊庫出錯,詳細錯誤為:" + ex.Message);74         }75     }76 77     public Workbook NewExcel()78     {79         DataTable table = (DataTable)Session["Books"];80         Workbook newWorkBook = new Workbook();81         Aspose.Cells.Worksheet sheet = newWorkBook.Worksheets[0];82         Cells cells = sheet.Cells;83 84         Aspose.Cells.Style style = newWorkBook.Styles[newWorkBook.Styles.Add()];//新增樣式85         style.HorizontalAlignment = TextAlignmentType.Center;86         style.Font.Size = 14;87         style.Font.Color = System.Drawing.Color.Red;88         cells.SetRowHeight(0, 20);89 90         for (int i = 0; i < table.Columns.Count; i++)91         {92             cells[0, i].PutValue(table.Columns[i].ColumnName);93             cells[0, i].SetStyle(style);94             cells.SetColumnWidthPixel(i, 150);95         }96         return newWorkBook;97     }

 

Asp.net & Aspose.cells 匯出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.