zoukankan      html  css  js  c++  java
  • Asp.Net中应用Aspose.Cells输出报表到Excel 及样式设置

    解决思路:

    1、找个可用的Aspose.Cells(有钱还是买个正版吧,谁开发个东西也不容易);

    2、在.Net方案中引用此Cells;

    3、写个函数ToExcel(传递一个DataTable),可以另写个SetCellStyle的函数专门给各列设置样式。

    4、在按钮的Click事件中调用此ToExcel()即可。

    注:想更详细了解的到Aspose网站去,那里有很多在线文档,够你看的了。也可以下载个Demo程序研究。

    部分代码贴在这里,代码还有需要改进的地方,暂时这么用着,给自己以后方便查找,再者给真正接触此控件的同志们抛个砖头:

    ExportToExcel()的:

    [csharp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. /// <summary>  
    2.         /// 利用Aspose.Cells对DataTable数据生成Excel文件  
    3.         /// </summary>  
    4.         /// <returns>是否成功</returns>  
    5.         /// <param name="response">包含存储路径,直接用Response即可</param>  
    6.         /// <param name="dt">数据体</param>  
    7.         /// <param name="FileName">文件名</param>  
    8.         /// <param name="SheetName">sheet名</param>  
    9.         /// <param name="Title">表头</param>  
    10.         /// <param name="ColTitle">列标题,字符串数组</param>  
    11.         /// <param name="ColName">列名,字符串数组</param>  
    12.         /// <param name="ColWidth">列宽,整数数组</param>  
    13.         /// <param name="ColStyle">列样式,整数数组,1=居中文本、2=特殊字体Georgia、3=居左文本、4=整数(x)、5=2位小数(x,xxx.xx)、6=日期(yyyy-M-d不带时间)、7=百分数(0.123=12.3%)</param>  
    14.         /// <param name="ColTitleRow">列标题的开始行号,有title的设为1即可</param>  
    15.         /// <param name="err">返回的错误信息</param>  
    16.         public bool ExportToExcel(System.Web.HttpResponse response,DataTable dt, string FileName, string SheetName, string Title,   
    17.             ArrayList ColTitle, ArrayList ColName, ArrayList ColWidth, ArrayList ColStyle, int ColTitleRow, ref string err)  
    18.         {  
    19.   
    20.             //先检查各数组是否个数一致  
    21.             if (ColTitle.Count != ColName.Count || ColTitle.Count != ColWidth.Count || ColTitle.Count != ColStyle.Count)  
    22.             {  
    23.                 err = "数据组个数不一致";  
    24.                 return false;  
    25.             }  
    26.   
    27.             try  
    28.             {  
    29.   
    30.                 Workbook workbook = new Workbook();   
    31.   
    32.                 //打开模版文件  
    33.                 //          string path = System.Web.HttpContext.Current.Server.MapPath("~");  
    34.                 //          path = path.Substring(0, path.LastIndexOf("//"));         
    35.                 //          path += @"/designer/Workbooks/NumberFormatting.xls";                  
    36.                 //          workbook.Open(path);  
    37.   
    38.                 //打开sheet  
    39.                 workbook.Worksheets.Clear();  
    40.                 Worksheet worksheet = workbook.Worksheets.Add(SheetName);  
    41.                 worksheet = workbook.Worksheets[0];  
    42.   
    43.                 Cells cells = worksheet.Cells;   
    44.                 //cells.ClearContents(0,0,60,30);   
    45.   
    46.                 //加入样式  
    47.                 ArrayList styles = new ArrayList();  
    48.                 styles = SetCellStyle(workbook, ColStyle);  
    49.   
    50.                 //加入Title即表头,类似“xxx汇总表”,其占居所有列数  
    51.                 Range w;//范围  
    52.                 if(ColTitleRow>0)  
    53.                 {  
    54.                     //Put数据到某个cell中  
    55.                     cells[0,0].PutValue(Title);  
    56.                     //设置行高  
    57.                     cells.SetRowHeight(0, 35);  
    58.                     //合并单元格  
    59.                     cells.Merge(0,0,1,(byte)ColName.Count);  
    60.                     //CreateRange函数参数说明:cells.CreateRange(firstrow, firstcol, rownumber, colnumber)  
    61.                     w=cells.CreateRange(0,0,1,(byte)ColName.Count);  
    62.                     //设置该合并单元的Style  
    63.                     //w.Style = (Aspose.Cells.Style)styles[ColStyle.Count];  
    64.                     w.Style = workbook.Styles["SheetTitle"];  
    65.                     }  
    66.               
    67.                 //给各列的标题行PutValue,类似“序号,类型,名称,价格,数量,合计”  
    68.                 int currow = ColTitleRow;             
    69.                 byte curcol = 0;  
    70.                 foreach(string s in ColTitle)  
    71.                 {  
    72.                     cells[currow,curcol++].PutValue(s);  
    73.                     cells.SetRowHeight(ColTitleRow, 25);  
    74.                 }  
    75.                 //设置列标题行的Style  
    76.                 w=cells.CreateRange(currow,0,1,ColName.Count);                        
    77.                 w.Style = (Aspose.Cells.Style)styles[ColStyle.Count+1];   
    78.                 //上面这行也可以写成  
    79.                 //w.Style = workbook.Styles["ColTitle"];//ColTitle在函数SetCellStyle中设置了  
    80.   
    81.                 currow++;  
    82.               
    83.   
    84.                 //将数据体按顺序插入各cell  
    85.                 for(int i=0; i<dt.Rows.Count; i++)  
    86.                 {  
    87.                     curcol = 0;  
    88.                     for(int j=0; j<ColName.Count; j++)  
    89.                     {  
    90.                         object val = dt.Rows[i][ColName[j].ToString()].ToString().Trim();  
    91.                         switch (int.Parse(ColStyle[j].ToString()))  
    92.                         {  
    93.                             case 4://整数  
    94.                                 if (val.ToString().Length>0)  
    95.                                     val = Int32.Parse(val.ToString());  
    96.                                 else  
    97.                                     val = "";  
    98.                                 break;  
    99.                             case 5://2位小数  
    100.                                 if (val.ToString().Length>0)  
    101.                                     val = Decimal.Parse(val.ToString());  
    102.                                 else  
    103.                                     val = "";  
    104.                                 break;  
    105.                             case 6://日期  
    106.                                 if (val.ToString().Length>0)  
    107.                                     val = DateTime.Parse(val.ToString());  
    108.                                 else  
    109.                                     val = "";  
    110.                                 break;  
    111.                             case 7://百分数,1=100%  
    112.                                 if (val.ToString().Length>0)  
    113.                                     val = Decimal.Parse(val.ToString());  
    114.                                 else  
    115.                                     val = "";  
    116.                                 break;  
    117.                             default:  
    118.                                 break;  
    119.                         }//end switch         
    120.                         cells[currow,curcol++].PutValue(val);  
    121.                     }//end for j  
    122.                     currow ++;  
    123.                 } //end for i  
    124.                 curcol = 0;  
    125.                 //设置数据体Style  
    126.                 for(int i=0; i<dt.Columns.Count; i++)  
    127.                 {  
    128.                     w = cells.CreateRange(ColTitleRow+1, i, dt.Rows.Count, 1);  
    129.                     w.Style = (Aspose.Cells.Style)styles[i];                  
    130.                 }  
    131.                 //w=cells.CreateRange(ColTitleRow+1,col,currow,ColName.Count);  
    132.                 //w.Style=excel.Styles["Data"];  
    133.   
    134.                 //设置各列宽度  
    135.                 foreach(int s in ColWidth)  
    136.                     cells.SetColumnWidth(curcol++, s);  
    137.   
    138.                 /* 
    139.                 //********可参考的格式设置*******************************************************                
    140.                 //Set number format with built-in index 
    141.                 for (int i = 0; i < 37; i ++) 
    142.                 { 
    143.                     cells[i, 1].PutValue(1234.5); 
    144.                     //int Number = cells[i, 0].IntValue; 
    145.  
    146.                     //Set the display number format 
    147.                     cells[i, 1].Style.Number = i; 
    148.                 } 
    149.  
    150.                 //Set number format with custom format string 
    151.                 for (int i = 1; i < 4; i ++) 
    152.                 { 
    153.                     cells[i, 3].PutValue(1234.5); 
    154.                  
    155.                     //Set the display custom number format 
    156.                     cells[i, 3].Style.Custom = cells[i, 2].StringValue; 
    157.                 } 
    158.                 //********可参考的格式设置******************************************************* 
    159.                 */  
    160.   
    161.                 //workbook.Save(FileName, FileFormatType.Default, SaveType.OpenInExcel, System.Web.HttpContext.Current.Response);  
    162.                 workbook.Save(FileName, FileFormatType.Default, SaveType.OpenInExcel, response);  
    163.   
    164.                 return true;  
    165.             }  
    166.             catch (Exception ex)  
    167.             {  
    168.                 err = ex.Message;  
    169.                 return false;  
    170.             }  
    171.         }//end ExportToExcel()  
    172.         #endregion   

    样式设置SetStyle()的:

    [csharp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #region SetCellStyle()设置格式,如果需要增加新的格式,请在case后面增加,不要修改前面的  
    2. public ArrayList SetCellStyle(Workbook workbook, ArrayList styleindex)  
    3. {  
    4.     //通用设置样式的  
    5.     ArrayList CellStyle = new ArrayList(styleindex.Count + 2);  
    6.     Aspose.Cells.Style style = null;  
    7.     for (int i=0; i<styleindex.Count; i++)  
    8.     {  
    9.         int index = workbook.Styles.Add();  
    10.           
    11.         style = workbook.Styles[index];  
    12.         style.Name = "Custom_Style" + ((int)(i + 1)).ToString();  
    13.         style.ForegroundColor = Color.White;  
    14.         style.HorizontalAlignment = TextAlignmentType.Center;  
    15.         style.VerticalAlignment = TextAlignmentType.Center;  
    16.         style.Font.Name = "宋体";  
    17.         style.Font.Size = 10;  
    18.         style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;  
    19.         style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;  
    20.         style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;  
    21.         style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;  
    22.           
    23.         switch((int)styleindex[i])  
    24.         {  
    25.             case 1://居中文本  
    26.                 style.HorizontalAlignment = TextAlignmentType.Center;  
    27.                 style.VerticalAlignment = TextAlignmentType.Center;  
    28.                 break;  
    29.             case 2://特殊字体Georgia  
    30.                 style.Font.Name = "Georgia";  
    31.                 break;  
    32.             case 3://文本列  
    33.                 style.HorizontalAlignment = TextAlignmentType.Left;  
    34.                 break;  
    35.             case 4://整数列 1 Decimal 0    
    36.                 style.HorizontalAlignment = TextAlignmentType.Center;  
    37.                 style.Number = 1;  
    38.                 break;  
    39.             case 5://2位小数 39={Currency #,##0.00;-#,##0.00}   40={#,##0.00;[Red]-#,##0.00}  
    40.                 style.HorizontalAlignment = TextAlignmentType.Right;  
    41.                 style.Number = 40;  
    42.                 break;  
    43.             case 6://日期列 14 Date yyyy-m-d  
    44.                 style.HorizontalAlignment = TextAlignmentType.Center;  
    45.                 style.Number = 14;//这个格式不是太好,还需要调整  
    46.                 break;  
    47.             case 7://百分比% 10 Percentage 0.00%     
    48.                 style.HorizontalAlignment = TextAlignmentType.Center;  
    49.                 style.Number = 10;  
    50.                 break;  
    51.             default:  
    52.                 break;  
    53.         }//end switch  
    54.         CellStyle.Add(style);  
    55.     }  
    56.   
    57.     //特别增加一个用于表头的style.Name = "SheetTitle";  
    58.     int sindex = workbook.Styles.Add();  
    59.     style = workbook.Styles[sindex];  
    60.     style.Name = "SheetTitle";  
    61.     style.Font.Size = 14;  
    62.     style.Font.IsBold = true;  
    63.     style.Font.Name = "楷体_GB2312";  
    64.     style.HorizontalAlignment = TextAlignmentType.Center;  
    65.     style.VerticalAlignment = TextAlignmentType.Center;  
    66.     style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;  
    67.     style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;  
    68.     style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;  
    69.     style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;  
    70.   
    71.     CellStyle.Add(style);  
    72.   
    73.     //特别增加一个用于列标题的style.Name = "ColTitle";  
    74.     sindex = workbook.Styles.Add();  
    75.     style = workbook.Styles[sindex];  
    76.     style.Name = "ColTitle";  
    77.     style.Font.Size = 12;  
    78.     style.Font.IsBold = true;  
    79.     style.Font.Name = "宋体";  
    80.     style.HorizontalAlignment = TextAlignmentType.Center;  
    81.     style.VerticalAlignment = TextAlignmentType.Center;  
    82.     style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;  
    83.     style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;  
    84.     style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;  
    85.     style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;  
    86.   
    87.     CellStyle.Add(style);  
    88.   
    89.     //返回样式数组  
    90.     return CellStyle;  
    91.       
    92. }//end SetCellStyle  
    93. #endregion  
  • 相关阅读:
    online ddl与pt-osc详解
    几个重点问题回顾
    死锁及常见死锁模型
    InnoDB中锁的算法(3)
    一个幻读模型引出的记录可见性判断
    jupyter notebook的使用
    l线程池抓取lianjia
    lagou数据爬取
    爬虫代理的设置
    linux如何安装和启动mongdb
  • 原文地址:https://www.cnblogs.com/GmrBrian/p/6223176.html
Copyright © 2011-2022 走看看