Asp.net & Aspose.cells 导出

 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         try
14         {
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     }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。