• C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)


    在项目中用到,特此记录下来,Aspose.Cells 不依赖机器装没有装EXCEL都可以导出,很方便。具体可以参考其他

    http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets#ImportingDatatoWorksheets-array

    Webfrom版本:

    效果如下:

    [csharp] view plain copy
     
    1.   protected void btnAsnExport_ServerClick(object sender, EventArgs e)  
    2.           {  
    3.                var getAsnData = SearchDataClass.GetAsnSearchData(txtAsnNo.Value,  
    4.                hfCustomerID.Value, txtTimeSelect.Value,txtSku.Value,txtSkuContent.Value);  
    5.   
    6. //设置导出excel列的标题  
    7.                ArrayList ColTitle = new ArrayList()   
    8.                     { "ASN编号", "SKU", "产品描述", "预期数量", "收货数量",  
    9.                      "单位","收货库位","收货时间","所属客户","ASN状态","ASN创建时间" };  
    10.   
    11.                //string[] strTitle = new string[] { "ASNNo", "SKU", "SKUDescrC", "ExpectedQty", "ReceivedQty", "UOM",  
    12.  "ReceivingLocation", "ReceivedTime", "CustomerID", "CodeName_C" };  
    13.                if (getAsnData.ToList().Count > 0)  
    14.                {  
    15.                     Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();  
    16.                     //创建一个sheet  
    17.                     Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];  
    18.                     //为单元格添加样式        
    19.                     Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];  
    20.                     style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;  
    21.                     style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线  
    22.                     style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线  
    23.                     style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线  
    24.                     style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线   
    25.   
    26.                     //给各列的标题行PutValue赋值  
    27.                     int currow = 0;  
    28.                     byte curcol = 0;  
    29.                     //sheet.Cells.ImportCustomObjects((System.Collections.ICollection)getAsnData,  
    30.                     //strTitle, true, 0, 0, getAsnData.Count, true, "yyyy/MM/dd HH:mm", false);  
    31.                     sheet.Cells.ImportCustomObjects((System.Collections.ICollection)getAsnData,  
    32.                     null, true, 0, 0, getAsnData.Count, true, "yyyy/MM/dd HH:mm", false);  
    33.                     // 设置内容样式  
    34.                     for (int i = 0; i < getAsnData.ToList().Count; i++)  
    35.                     {  
    36.                          for (int j = 0; j < 11; j++)  
    37.                          {  
    38.                               sheet.Cells[i + 1, j].Style = style;  
    39.                               sheet.Cells[i + 1, 2].Style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Left;  
    40.                               sheet.Cells[i + 1, 7].Style.Custom = "yyyy/MM/dd HH:mm";  
    41.                               sheet.Cells[i + 1, 10].Style.Custom = "yyyy/MM/dd HH:mm";  
    42.                          }  
    43.                     }  
    44.                     // 设置标题样式及背景色  
    45.                     foreach (string s in ColTitle)  
    46.                     {  
    47.                          sheet.Cells[currow, curcol].PutValue(s);  
    48.                          style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);  
    49.                          style.Pattern = Aspose.Cells.BackgroundType.Solid;  
    50.                          style.Font.IsBold = true;  
    51.                          sheet.Cells[currow, curcol].Style = style;  
    52.                          curcol++;  
    53.                     }  
    54.   
    55.                     Aspose.Cells.Cells cells = sheet.Cells;  
    56.                     //设置标题行高  
    57.                     cells.SetRowHeight(0, 30);  
    58.                     //让各列自适应宽度  
    59.                     sheet.AutoFitColumns();  
    60.                     //生成数据流  
    61.                     System.IO.MemoryStream ms = workbook.SaveToStream();  
    62.                     byte[] bt = ms.ToArray();  
    63.                     //客户端保存的文件名  
    64.                     string fileName = "入库查询数据导出" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";  
    65.                     //以字符流的形式下载文件    
    66.                     Response.ContentType = "application/vnd.ms-excel";  
    67.                     //通知浏览器下载文件而不是打开  
    68.                     Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));  
    69.                     Response.BinaryWrite(bt);  
    70.                     Response.Flush();  
    71.                     Response.End();  
    72.                }  
    73.           }  

    Winform版本:

    效果图如下:


    [csharp] view plain copy
     
      1. public void ExportExcelWithAspose(MDataTable dt, string fileName)  
      2.       {  
      3.            string saveFileName = "";  
      4.            SaveFileDialog saveDialog = new SaveFileDialog();  
      5.            saveDialog.DefaultExt = "xls";  
      6.            saveDialog.Filter = "Excel文件|*.xls";  
      7.            saveDialog.FileName = fileName;  
      8.            saveDialog.ShowDialog();  
      9.            saveFileName = saveDialog.FileName;  
      10.            if (saveFileName.IndexOf(":") < 0) return; //被点了取消  
      11.            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();  
      12.            if (xlApp == null)  
      13.            {  
      14.                 MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");  
      15.                 return;  
      16.            }  
      17.       
      18.            try  
      19.            {  
      20.                 Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;  
      21.                 Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();  
      22.                 Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];  
      23.                 Aspose.Cells.Cells cells = cellSheet.Cells ;//单元格  
      24.                 //为单元格添加样式        
      25.                 Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];  
      26.                 //设置居中    
      27.                 style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;  
      28.                 //行索引  
      29.                 int rowIndex = 0;  
      30.                 //列索引  
      31.                 int colIndex = 0;  
      32.                 //列总数  
      33.                 int colCount = dt.Columns.Count;  
      34.                 //总行数  
      35.                 int rowCount = dt.Rows.Count;  
      36.               
      37.                 rowIndex++;  
      38.                 for (int i = 0; i < rowCount; i++)  
      39.                 {  
      40.                      colIndex = 0;  
      41.                      for (int j = 0; j < colCount; j++)  
      42.                      {  
      43.                           if (j == 5) { cellSheet.Cells[rowIndex, colIndex].PutValue(Convert.ToDateTime (dt.Rows[i][j].Value).ToString("yyyy/MM/dd HH:mm:ss")); }  
      44.                           else { cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].Value); }  
      45.                           style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线  
      46.                           style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线  
      47.                           style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线  
      48.                           style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线   
      49.                           cellSheet.Cells[rowIndex, colIndex].Style = style;  
      50.                           colIndex++;  
      51.                      }  
      52.                      rowIndex++;  
      53.                 }  
      54.                 //清除内容时行列索引值为0  
      55.                 rowIndex = 0; colIndex = 0;  
      56.                 //列名的处理    
      57.                 for (int i = 0; i < colCount; i++)  
      58.                 {  
      59.                      cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);  
      60.                      //设置背景颜色    
      61.                      style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);  
      62.                      style.Pattern = Aspose.Cells.BackgroundType.Solid;  
      63.                      style.Font.IsBold = true;  
      64.                      style.IsTextWrapped = true;  
      65.                      cells.SetRowHeight(0, 38);//设置行高  
      66.                      cellSheet.Cells[rowIndex, colIndex].Style = style;  
      67.                      colIndex++;  
      68.                 }  
      69.                 cellSheet.AutoFitColumns();  
      70.                 workbook.Save(Path.GetFullPath(saveFileName));  
      71.                 xlApp.Quit();  
      72.                 GC.Collect();//强行销毁  
      73.                 MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK,MessageBoxIcon.Information);  
      74.            }  
      75.            catch (Exception ex)  
      76.            {  
      77.                 MessageBox.Show("导出文件时出错,文件可能正被打开! " + ex.Message);  
      78.            }  
      79.       }    
  • 相关阅读:
    开启linux服务器防火墙
    Linux系统编程11_管道和命名管道
    Lua语法
    Git学习
    Buildroot介绍
    Makefile基本介绍
    页、页表和块
    文件系统,根文件系统,MTD
    什么是ioctl
    bootargs的mtdparts解析
  • 原文地址:https://www.cnblogs.com/shiyh/p/7478235.html
Copyright © 2020-2023  润新知