使用Aspose.Cells導出導入excel

//導出excel
 private void btn_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "導出Excel";
            saveFileDialog.Filter = "Excel文件(*.xls)|*.xls|Excel文件(*.xlsx)|*.xlsx";
            var Result = saveFileDialog.ShowDialog();
            if (Result == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    List<MergeEntity> data = gridControl1.DataSource as List<MergeEntity>;//數據源
                    var license = new Aspose.Cells.License();
                    license.SetLicense(AppDomain.CurrentDomain.BaseDirectory + "temp/Aspose.Total.lic");//加載license
                    Workbook work = new Workbook();
                    Worksheet sheet = work.Worksheets[0];
                    Cells cell = sheet.Cells;
                    //設置表頭樣式
                    Aspose.Cells.Style styleHeader = work.Styles[work.Styles.Add()];
                    styleHeader.Pattern = Aspose.Cells.BackgroundType.Solid;
                    styleHeader.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
                    styleHeader.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
                    styleHeader.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
                    styleHeader.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
                    styleHeader.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
                    styleHeader.Font.IsBold = true;//粗體
                    styleHeader.Font.Size = 12;//字體大小
                    //設置列標題
                    cell[0, 0].PutValue("列名1");
                    cell[0, 1].PutValue("列名2");
                    cell[0, 2].PutValue("列名3");
                    cell[0, 3].PutValue("列名4");
                    cell[0, 4].PutValue("列名5");
                    cell[0, 5].PutValue("列名6");
                    for (int i = 0; i < 6; i++)
                    {
                        cell[0, i].SetStyle(styleHeader);//設置表頭樣式
                        cell.SetColumnWidth(i+1, 20);//設置表頭寬度
                    }
                    for (int i = 0; i < data.Count; i++)
                    {
                        cell[i + 1, 0].PutValue(data[i].Name1);
                        cell[i + 1, 1].PutValue(data[i].Name2);
                        cell[i + 1, 2].PutValue(data[i].Name2);
                        cell[i + 1, 3].PutValue(data[i].Name3);
                        cell[i + 1, 4].PutValue(data[i].Name4);
                        cell[i + 1, 5].PutValue(data[i].Name5);
                    }
                    //cell.Merge(0,0, 0, 0);合併單元格
                    work.Save(saveFileDialog.FileName);
                    if (System.Windows.MessageBox.Show("導出成功,是否打開文件?", "詢問", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        System.Diagnostics.Process.Start(saveFileDialog.FileName);
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message);
                }
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章