ASP.NET报表问题的解决方法(附源码)

开发 后端
做ASP.NET项目都会碰到ASP.NET报表问题,中国式报表的繁琐让我很是反感,可没法子啊,客户有需求!后来有人推荐了个控件ASPOSE解决ASP.NET报表问题,这玩意强,服务端都不用装EXCEL,效果不错!想偷懒的用这玩意,一个字——爽!

做ASP.NET项目都会碰到ASP.NET报表问题,中国式报表的繁琐让我很是反感,可没法子啊,客户有需求!自然而然想用DCOM直接操作EXCEL,马马虎虎算是成了,虽说安全性不好,资源浪费严重,毕竟在局域网中使用,凑合吧。

不过总是讨厌在服务端那个没有结束的EXCEL进程……一定要把敌人杀死!google了一把,原来当今中外都有这个ASP.NET报表问题。解决方案也有,Windows XP下搞定,但我在WINDOWS 2003下搞了整整一天也没杀死,得,真想自个杀了。后来有人推荐了个控件ASPOSE解决ASP.NET报表问题,这玩意强,服务端都不用装EXCEL,效果不错!想偷懒的用这玩意,一个字——爽! 

  1. private string subTotal(int row ,int num)  
  2.         {   
  3.             string r = (row+1).ToString();  
  4.          if( num == 0)//  
  5.          {  
  6.              return "=C"+r+"+F"+r+"-I"+r;  
  7.          }  
  8.         else 
  9.             return "=E"+r+"+H"+r+"-K"+r;  
  10.               
  11.         }  
  12.         private string sumTotal(string scol,int firstrow,int num){  
  13.  
  14.         string r = num.ToString();  
  15.             string fr = firstrow.ToString();  
  16.             return "=Sum("+scol+fr+":"+scol+r+")";  
  17.  
  18.         }  
  19.         private void CreateExcelWorkbook(DataRow[] rows)  
  20.         {  
  21.             string strCurrentDir = Server.MapPath("..") + "\\TempReports\\";  
  22.             string licenseFile = MapPath("..") + "\\XML\\Aspose.Excel.lic";               
  23.             Excel excel = new Excel(licenseFile, this);  
  24.         ;  
  25.             string designerFile = strCurrentDir+"cangku2.xls";  
  26.                         excel.Open(designerFile);  
  27.             Worksheet sheet = excel.Worksheets["Sheet1"];  
  28.             sheet.Name = "wuzi";  
  29.             Cells cells = sheet.Cells;  
  30.             int styleIndex;  
  31.               
  32.             styleIndex = excel.Styles.Add();  
  33.             Aspose.Excel.Style stylecell = excel.Styles[styleIndex];  
  34.             stylecell.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;  
  35.             stylecell.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;  
  36.             stylecell.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;  
  37.             stylecell.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;  
  38.             int iRow =4;  
  39.             foreach(DataRow row in rows)  
  40.             {  
  41.             cells[iRow,0].PutValue(row[0].ToString());  
  42.             cells[iRow,1].PutValue(row[1].ToString());  
  43.             cells[iRow,2].PutValue(row[2]);  
  44.             cells[iRow,3].PutValue( row[3]);  
  45.             cells[iRow,4].PutValue(row[4]);  
  46.             cells[iRow,5].PutValue(row[5]);  
  47.             cells[iRow,6].PutValue(row[3]);  
  48.             cells[iRow,7].PutValue(row[6]);  
  49.             cells[iRow,8].PutValue(row[7]);  
  50.             cells[iRow,9].PutValue(row[3]);  
  51.             cells[iRow,10].PutValue(row[8]);  
  52.             cells[iRow,11].Formula=subTotal(iRow,0);  
  53.             cells[iRow,12].PutValue(row[3]);  
  54.             cells[iRow,13].Formula=subTotal(iRow,1);  
  55.             iRow++;  
  56.                 }  
  57.             string zj="总计";  
  58.                 cells[iRow,3].PutValue(zj);  
  59.                  cells[iRow,6].PutValue(zj);  
  60.                  cells[iRow,9].PutValue(zj);  
  61.                cells[iRow,12].PutValue(zj);  
  62.                 cells[iRow,4].Formula=sumTotal("E",5,iRow);  
  63.                 cells[iRow,7].Formula=sumTotal("H",5,iRow);  
  64.                 cells[iRow,10].Formula=sumTotal("K",5,iRow);  
  65.                 cells[iRow,13].Formula=sumTotal("N",5,iRow);  
  66.                 cells[1,8].PutValue(cpStartDate.SelectedDate.ToShortDateString());  
  67.                 cells[1,12].PutValue(cpEndDate.SelectedDate.ToShortDateString());  
  68.             Range range = cells.CreateRange(4,0,iRow-4+1,14);  
  69.                 range.Style = stylecell;  
  70.             for(int i = 0; i < excel.Worksheets.Count ; i ++)  
  71.             {  
  72.                 sheet = excel.Worksheets[i];  
  73.                 if(sheet.Name != "wuzi")  
  74.                 {  
  75.              excel.Worksheets.RemoveAt(i);  
  76.                     i --;  
  77.                 }  
  78.  
  79.             }  
  80.                 /*Response.Clear();  
  81.           Response.Buffer= true;  
  82.           Response.Charset="GB2312";  
  83.  
  84.         this.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");*/ 
  85.               
  86.         excel.Save(HttpUtility.UrlEncode("物资进出库汇总.xls",Encoding.UTF8), SaveType.OpenInBrowser,FileFormatType.Default,this.Response);  
  87. }  

【编辑推荐】

  1. 浅谈ASP.NET报表控件
  2. ASP.NET环境下的Shell函数
  3. 在ASP.NET中向数据库批量插入数据
  4. ASP.NET用Post方式向网页发送数据
  5. ASP.NET 2.0部署WEB应用程序浅析
  6. ASP.NET中的HttpWorkerRequest对像
责任编辑:老杨 来源: 模板天下
相关推荐

2009-07-22 17:50:14

2009-07-27 16:19:59

ASP.NET报表控件

2009-07-29 09:29:06

ASP.NET水晶报表

2009-07-23 10:06:29

2009-07-28 12:35:37

querystring

2009-07-28 13:26:34

Render方法ASP.NET

2009-08-06 15:56:40

ASP.NET Coo

2009-08-12 18:19:46

ASP.NET报表打印

2009-08-05 18:47:55

ASP.NET服务器

2009-07-20 16:04:37

ASP.NET fra

2009-07-22 09:11:02

Action方法ASP.NET MVC

2009-08-02 11:48:58

ASP.NET水晶报表ASP.NET

2009-07-30 13:57:39

ASP.NET水晶报表ASP.NET

2009-08-25 17:42:41

ASP.NET新手问题

2009-07-27 17:51:58

WCF服务ASP.NET

2012-09-20 09:54:12

ASP.NETWebC

2009-07-23 10:08:24

asp.net mvc

2009-07-29 14:02:41

ASP.NET项目

2009-07-28 17:17:19

ASP.NET概述

2009-08-03 14:22:33

什么是ASP.NET
点赞
收藏

51CTO技术栈公众号