运用Aspose.dll导出EXCEL表格

using System.IO.Ports;
using System.Data.SqlClient;
using Aspose.Cells;
using System.Web;

using System.IO;

Aspose.dll需要收费,不过可以在网上找到很多破解版

 public void ExportExcelWithAspose(DataTable dt)
        {         
            string saveFileName = null;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel 工作簿|*.xls";        
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; //被点了取消  
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的设备未安装Excel");
                return;
            }
            try
            {
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];
                Aspose.Cells.Cells cells = cellSheet.Cells;//单元格  
                //为单元格添加样式        
                Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
                Aspose.Cells.Style styletitle = workbook.Styles[workbook.Styles.Add()];
                //设置居中    
                style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
                style.VerticalAlignment = Aspose.Cells.TextAlignmentType.Center;
                styletitle.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
                styletitle.VerticalAlignment = Aspose.Cells.TextAlignmentType.Center;
                //行索引  
                int rowIndex = 0;
                //列索引  
                int colIndex = 0;
                //列总数  
                int colCount = 0;
                colCount = dt.Columns.Count;
                //总行数  
                int rowCount = 0;
                rowCount = dt.Rows.Count;


                rowIndex++;          
                for (int i = 0; i < rowCount; i++)
                {
                    colIndex = 0;
                    for (int j = 0; j < colCount; j++)
                    {
                        cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString());
                        style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线  
                        style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线  
                        style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线  
                        style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线   
                        cellSheet.Cells[rowIndex, colIndex].Style = style;
                        colIndex++;
                    }
                    rowIndex++;
                }              
                //清除内容时行列索引值为0  
                rowIndex = 0; colIndex = 0;
                //列名的处理    
                for (int i = 0; i < colCount; i++)
                {
                    cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
                    styletitle.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线  
                    styletitle.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线  
                    styletitle.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线  
                    styletitle.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
                    //设置背景颜色                  
                    styletitle.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
                    styletitle.Pattern = Aspose.Cells.BackgroundType.Solid;
                    styletitle.Font.IsBold = true;
                    styletitle.IsTextWrapped = true;
                    cells.SetRowHeight(0, 30);//设置行高  
                    cellSheet.Cells[rowIndex, colIndex].Style = styletitle;
                    colIndex++;
                }
                cellSheet.AutoFitColumns();
                workbook.Save(Path.GetFullPath(saveFileName));
                //string fileNameExt = saveFileName.Substring(saveFileName.LastIndexOf("\\") + 1);
                //MessageBox.Show("" + fileNameExt + " 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
            }
            xlApp.Quit();
            GC.Collect();//强行销毁           
        }

猜你喜欢

转载自blog.csdn.net/qq_36675669/article/details/80946717