温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么进行ASP.NET Excel动态实现

发布时间:2021-10-28 10:53:03 来源:亿速云 阅读:125 作者:柒染 栏目:编程语言

今天就跟大家聊聊有关怎么进行ASP.NET Excel动态实现,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

ASP.NET Excel动态实现首先在Asp.net中建立本地的Excel表,并由服务器向外传播是容易实现的,而删除掉嵌入的Excel.exe进程是困难的。所以 你不要打开任务管理器 ,看Excel.exe进程相关的东西是否还在内存里面。我在这里提供一个解决方案 ,里面提供了两个方法 :

"CreateExcelWorkbook"(说明 建立ASP.NET Excel动态工作簿) 这个方法 运行一个存储过程 ,返回一个DataReader 并根据DataReader 来生成一个Excel工作簿 ,并保存到文件系统中,创建一个“download”连接,这样 用户就可以将Excel表导入到浏览器中也可以直接下载到机器上。

第二个方法:GenerateCSVReport 本质上是做同样的一件事情,仅仅是保存的文件的CSV格式 。仍然 导入到Excel中,CSV代码能解决一个开发中的普片的问题:你有一列 里面倒入了多个零,CSV代码能保证零不变空 。(说明: 就是在Excel表中多个零的值 不能保存的问题)

在可以下载的解决方案中,包含一个有效的类 ” SPGen” 能运行存储过程并返回DataReader ,一个移除文件的方法 能删除早先于一个特定的时间值。下面出现的主要的方法就是CreateExcelWorkbook

注意:你必须知道 在运行这个页面的时候,你可能需要能在WebSever 服务器的文件系统中写 Excel,Csv文件的管理员的权限。处理这个问题的最简单的方法就是运行这个页面在自己的文件夹里面并包括自己的配置文件。并在配置文件中添加下面的元素﹤identity impersonate ="true" ... 。你仍然需要物理文件夹的访问控制列表(ACL)的写的权限,只有这样运行的页面的身份有写的权限,***,你需要设置一个Com连接到Excel 9.0 or Excel 10 类型库 ,VS.NET 将为你生成一个装配件。我相信 微软在他们Office网站上有一个连接,可以下载到微软的初始的装配件 。(可能不准,我的理解是面向.net的装配件)

﹤identity impersonate="true" userName="adminuser" password="adminpass" /﹥

特别注意 下面的代码块的作用是清除ASP.NET Excel动态的对象。

// Need all following code to clean up and extingush all references!!!  oWB.Close(null,null,null);  oXL.Workbooks.Close();  oXL.Quit();  System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);  System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);  System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);  System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);  oSheet=null;  oWB=null;  oXL = null;  GC.Collect(); // force final cleanup!

这是必须的 ,因为oSheet", "oWb" , 'oRng", 等等 对象也是COM的实例,我们需要Marshal类的ReleaseComObject的方法把它们从.NET去掉

private void CreateExcelWorkbook(string spName, SqlParameter[] parms)  {  string strCurrentDir = Server.MapPath(".") + "";  RemoveFiles(strCurrentDir); // utility method to clean up old files   Excel.Application oXL;  Excel._Workbook oWB;  Excel._Worksheet oSheet;  Excel.Range oRng;    try {  GC.Collect();// clean up any other excel guys hangin' around...  oXL = new Excel.Application();  oXL.Visible = false;  //Get a new workbook.  oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));  oSheet = (Excel._Worksheet)oWB.ActiveSheet;  //get our Data    string strConnect = System.Configuration.ConfigurationSettings.AppSettings["connectString"];  SPGen sg = new SPGen(strConnect,spName,parms);   SqlDataReader myReader = sg.RunReader();   // Create Header and sheet...  int iRow =2;   for(int j=0;j﹤myReader.FieldCount;j++)  {  oSheet.Cells[1, j+1] = myReader.GetName(j).ToString();   }  // build the sheet contents  while (myReader.Read())  {   for(int k=0;k ﹤ myReader.FieldCount;k++)  {  oSheet.Cells[iRow,k+1]= myReader.GetValue(k).ToString();  }  iRow++;  }// end while  myReader.Close();  myReader=null;  //Format A1:Z1 as bold, vertical alignment = center.  oSheet.get_Range("A1", "Z1").Font.Bold = true;  oSheet.get_Range("A1", "Z1").VerticalAlignment =Excel.XlVAlign.xlVAlignCenter;  //AutoFit columns A:Z.  oRng = oSheet.get_Range("A1", "Z1");  oRng.EntireColumn.AutoFit();  oXL.Visible = false;  oXL.UserControl = false;  string strFile ="report" + System.DateTime.Now.Ticks.ToString() +".xls";  oWB.SaveAs( strCurrentDir + strFile,Excel.XlFileFormat.xlWorkbookNormal,       null,null,false,false,Excel.XlSaveAsAccessMode.xlShared,false,false,null,null,null);  // Need all following code to clean up and extingush all references!!!  oWB.Close(null,null,null);  oXL.Workbooks.Close();  oXL.Quit();  System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);  System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);  System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);  System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);  oSheet=null;  oWB=null;  oXL = null;  GC.Collect(); // force final cleanup!  string strMachineName = Request.ServerVariables["SERVER_NAME"];  errLabel.Text="﹤A href=http://" + strMachineName +"/ExcelGen/" +strFile + "﹥Download Report﹤/a﹥";    }  catch( Exception theException )   {  String errorMessage;  errorMessage = "Error: ";  errorMessage = String.Concat( errorMessage, theException.Message );  errorMessage = String.Concat( errorMessage, " Line: " );  errorMessage = String.Concat( errorMessage, theException.Source );   errLabel.Text= errorMessage ;  }  }

看完上述内容,你们对怎么进行ASP.NET Excel动态实现有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI