zoukankan      html  css  js  c++  java
  • 应用Aspose.Cells导入文件Execl

    1.应引用Aspose.Cells.dll类

      1    #region //导出文件
      2         private void Export(DataTable dt, string tableName)
      3         {
      4 
      5             Workbook workbook = new Workbook(); //工作簿 
      6             Worksheet sheet = workbook.Worksheets[0]; //工作表 
      7             Cells cells = sheet.Cells;//单元格 
      8 
      9 
     10             #region//表格样式
     11 
     12             //为标题设置样式    
     13             Aspose.Cells.Style style1 = workbook.Styles[workbook.Styles.Add()];//新增样式
     14             style1.HorizontalAlignment = TextAlignmentType.Center;//文字居中
     15             style1.Font.Name = "宋体";//文字字体
     16             style1.Font.Size = 16;//文字大小
     17             style1.Font.IsBold = true;//粗体
     18 
     19 
     20             //为标题设置样式2
     21             Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式 
     22             style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中 
     23             style2.Font.Name = "宋体";//文字字体 
     24             style2.Font.Size = 10;//文字大小 
     25             style2.Font.IsBold = true;//粗体 
     26             style2.IsTextWrapped = true;//自动换行
     27             style2.Pattern = BackgroundType.Solid; //设置背景样式
     28             style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //应用边界线 左边界线
     29             style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
     30             style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //应用边界线 上边界线
     31             style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //应用边界线 下边界线
     32 
     33             //为标题设置样式3
     34             Aspose.Cells.Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式 
     35             style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中 
     36             style3.Font.Name = "宋体";//文字字体 
     37             style3.Font.Size = 10;//文字大小 
     38             style3.IsTextWrapped = true;//自动换行
     39             style3.Pattern = BackgroundType.Solid; //设置背景样式
     40             style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //应用边界线 左边界线
     41             style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
     42             style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //应用边界线 上边界线
     43             style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //应用边界线 下边界线
     44 
     45             //字体样式
     46             Aspose.Cells.Style font1 = workbook.Styles[workbook.Styles.Add()];//新增样式
     47             font1.HorizontalAlignment = TextAlignmentType.Left;//文字居中
     48             font1.Font.Name = "宋体";//文字字体
     49             font1.Font.Size = 10;//文字大小
     50             font1.Font.IsBold = true;//粗体
     51             font1.IsTextWrapped = true;//自动换行
     52             font1.Pattern = BackgroundType.Solid; //设置背景样式
     53             font1.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //应用边界线 左边界线
     54             font1.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
     55             font1.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //应用边界线 上边界线
     56             font1.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //应用边界线 下边界线
     57 
     58             //字体样式
     59             Aspose.Cells.Style font2 = workbook.Styles[workbook.Styles.Add()];//新增样式
     60             font2.HorizontalAlignment = TextAlignmentType.Left;//文字居中
     61             font2.Font.Name = "宋体";//文字字体
     62             font2.Font.Size = 10;//文字大小
     63             font2.IsTextWrapped = true;//自动换行
     64             font2.Pattern = BackgroundType.Solid; //设置背景样式
     65             font2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //应用边界线 左边界线
     66             font2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
     67             font2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //应用边界线 上边界线
     68             font2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //应用边界线 下边界线
     69 
     70             #endregion
     71 
     72             int Rownum = dt.Rows.Count;//表格行数
     73 
     74             //生成行2 列名行
     75             cells[0, 0].PutValue("#");
     76             cells[0, 0].SetStyle(style2);
     77             cells[0, 1].PutValue("#");
     78             cells[0, 1].SetStyle(style2);
     79             cells[0, 2].PutValue("#");
     80             cells[0, 2].SetStyle(style2);
     81             cells[0, 3].PutValue("#");
     82             cells[0, 3].SetStyle(style2);
     83             cells[0, 4].PutValue("#");
     84             cells[0, 4].SetStyle(style2);
     85             cells[0, 5].PutValue("#");
     86             cells[0, 5].SetStyle(style2);
     87             cells[0, 6].PutValue("#");
     88             cells[0, 6].SetStyle(style2);
     89             cells[0, 7].PutValue("#");
     90             cells[0, 7].SetStyle(style2);
     91 
     92             cells.SetRowHeight(1, 35);
     93 
     94             for (int i = 0; i < Rownum; i++)
     95             {
     96 
     97                 cells[1 + i, 0].PutValue(dt.Rows[i]["#"].ToString());
     98                 cells[1 + i, 1].PutValue(dt.Rows[i]["#"].ToString());
     99                 cells[1 + i, 2].PutValue(dt.Rows[i]["#"].ToString());
    100                 cells[1 + i, 3].PutValue(dt.Rows[i]["#"].ToString());
    101                 cells[1 + i, 4].PutValue(dt.Rows[i]["#"].ToString());
    102                 cells[1 + i, 5].PutValue(dt.Rows[i]["#"].ToString());
    103                 cells[1 + i, 6].PutValue(dt.Rows[i]["#"].ToString());
    104                 cells[1 + i, 7].PutValue(dt.Rows[i]["#"].ToString());
    105                 for (int k = 0; k < 8; k++)
    106                 {
    107                     cells[1 + i, k].SetStyle(style3);
    108                 }
    109 
    110                 cells.SetRowHeight(1 + i, 30);
    111 
    112             }
    113             //setColumnWithAuto(sheet);
    114 
    115             Response.Clear();
    116             Response.Buffer = true;
    117             Response.Charset = "utf-8";
    118             string strTemp = System.Web.HttpUtility.UrlEncode(tableName, System.Text.Encoding.UTF8);//解决文件名乱码
    119             Response.AppendHeader("Content-Disposition", "attachment;filename=" + strTemp + ".xlsx");
    120             Response.ContentEncoding = System.Text.Encoding.UTF8;
    121             Response.ContentType = "application/ms-excel";
    122             Response.BinaryWrite(workbook.SaveToStream().ToArray());
    123             Response.End();
    124 
    125             webmsg.message("下载成功!");
    126         }
    127 
    128         /// <summary>       
    129         /// 
    130         /// 设置表页的列宽度自适应   
    131         /// /// </summary>       
    132         /// /// <param name="sheet">worksheet对象</param>  
    133         /// 
    134         public static void setColumnWithAuto(Worksheet sheet)
    135         {
    136             Cells cells = sheet.Cells;
    137             int columnCount = cells.MaxColumn;  //获取表页的最大列数   
    138             int rowCount = cells.MaxRow;        //获取表页的最大行数      
    139             for (int col = 0; col <= columnCount; col++)
    140             {
    141                 sheet.AutoFitColumn(col, 0, rowCount);
    142             }
    143             for (int col = 0; col <= columnCount; col++)
    144             {
    145                 int width = 0;
    146                 switch (col)
    147                 {
    148                     case 0: width = 120;
    149                         break;
    150                     case 1: width = 5;
    151                         break;
    152                     case 2: width = 5;
    153                         break;
    154                     case 3: width = 5;
    155                         break;
    156                     case 4: width = 50;
    157                         break;
    158                     case 5: width = 150;
    159                         break;
    160                     case 6: width = 50;
    161                         break;
    162                 }
    163 
    164                 cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + width);
    165             }
    166         }
    167         #endregion
  • 相关阅读:
    写在之前
    Fedora Core 3安装杂记(三)
    Fedora Core 3安装杂记(一)
    Firefox 1.0真的挺好用的
    发现Google加了英文页面翻译功能(Beta)
    Fedora Core 3安装杂记(四)
    在FC3的日子里……
    ASP面向对象编程探讨及比较
    显卡千万不能买带风扇的……
    字符串(strcat)
  • 原文地址:https://www.cnblogs.com/liliang2015/p/7199708.html
Copyright © 2011-2022 走看看