- 浏览: 109281 次
- 性别:
- 来自: 大连
文章分类
最新评论
-
wangming_1993:
[/img][/img][/img][/img][/img][ ...
nodejs express 上传文件 临时路径 -
beyond_开阔天空:
像scheme_menu中的scheme_second_men ...
内嵌数组操作 -
beyond_开阔天空:
请问一下,像这个子文档中的数组怎么插入、更新、删除{ &q ...
内嵌数组操作 -
yz285131311:
使用本地数据添加进 jqgrid, 那当它点击下一页这些时候能 ...
jqgrid 的一些操作 动态增加列 用本地 数据 修改表格 -
houyinke:
[/url][url][/url][url][/url][ur ...
java缩放图片
原理很简单:
1 copy模版文件到临时文件夹,并改名。(我用的是时间)
2 修改这个临时文件。(这部分还没有想好怎么作, 毕竟是C#菜鸟啊)
3 下载这个临时文件。
4 删除临时文件。
在系统中不会留下任何进程, 哈哈。
微软的东西, 你用到了就一定要关闭。
代码中 close()方法关闭掉了大部分。
但还有一些
private Excel.Range oRng;
对象没有关掉的话 系统中会留下 EXcel.exe 进程, 很危险啊!!!!
要引入
Microsoft.Office.Interop.Excel.dll 我的版本是 12.0.0.0
using System; using System.Data; using System.Configuration; using System.Diagnostics; using Excel = Microsoft.Office.Interop.Excel; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Web; /// <summary> /// 根据模版写Excel文件,并提供下载 /// </summary> public class ExcelComManager { private Excel.ApplicationClass ExcelObj = null; //Excel对象 private Excel._Workbook xBk; //工作薄 private Excel._Worksheet xSt; //工作Sheet private Excel.Range xRng; //操作单元格样式 private object missing = System.Reflection.Missing.Value; /// <summary> /// 模板文件名 完全路径 /// </summary> private string templateFilePath; /// <summary> /// 模版描述文件 完全路径 /// </summary> private string templateDiscriptionFilePath; public ExcelComManager() { } /// <summary> /// 根据Excel模版,创建Excel临时文件并传输 /// </summary> /// <param name="templatName">模版名</param> public void createExcelAndSend(string templatName, Object dataObject) { templateFilePath = getTemplatePath(templatName, ".xls"); //模板文件 templateDiscriptionFilePath = getTemplatePath(templatName, ".xml"); //模板描述文件 string tempFilePath = getTempFileName(); //临时文件全名 copyFile(templateFilePath, tempFilePath); //拷贝模版文件到临时文件夹 connectTempExcel(tempFilePath); //打开临时Excel文件 //这里应该根据不同的模版调用不同的写入方法 //还没有完成 writeExcelContent(dataObject); //操作临时Excel文件 sendExcelFile(tempFilePath, "cc.xls"); //发送文件到客户端 deleteTempFile(tempFilePath); //删除临时文件 } #region copy模版文件到指定目录 并改名 /// <summary> /// copy模版文件到指定目录 并改名 /// </summary> /// <param name="scrFile">要拷贝的原文件全路径名</param> /// <param name="destFile">要生成的目标文件的全路径名</param> /// <returns></returns> private void copyFile(string scrFile, string destFile) { try { System.IO.File.Copy(scrFile, destFile); } catch(Exception e) { throw new Exception("拷贝文件出错"); } } #endregion #region 删除临时Excel文件 /// <summary> /// 删除临时Excel文件 /// </summary> /// <param name="tempFile">临时文件全路径名</param> private void deleteTempFile(string tempFile) { try { System.IO.FileInfo file = new System.IO.FileInfo(tempFile); if(file.Exists){ file.Delete(); } } catch (Exception e) { throw new Exception("删除文件出错"); } } #endregion #region 打开临时Excel文件 /// <summary> /// 打开临时Excel文件,(copy过来的并改好名字的模版) /// </summary> /// <param name="filePath">Excel文件全路径名</param> public void connectTempExcel(string filePath) { try { ExcelObj = new Excel.ApplicationClass(); xBk = ExcelObj.Workbooks.Open(filePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); xSt = (Excel._Worksheet)xBk.ActiveSheet; } catch (Exception e) { close(); throw new Exception("打开Excel文件出错"); } } #endregion #region 连接Excel public void connectExcel() { try { object missing = System.Reflection.Missing.Value; //如果模版名为空 或 模版文件没有找到 则新建一个 if (string.IsNullOrEmpty(templateFilePath)) { xBk = ExcelObj.Workbooks.Add(true); } else { xBk = ExcelObj.Workbooks.Open(templateFilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); if(xBk == null){ throw new Exception("打开模版文件出错"); } } xBk.SaveAs(getTempFileName(), Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); xSt = (Excel._Worksheet)xBk.ActiveSheet; } catch (Exception e) { close(); throw; } } #endregion #region 根据数据结果集, 修改临时文件内容 /// <summary> /// 根据数据结果集,修改临时文件内容 /// </summary> /// <param name="ds">要传入到Excel的数据集</param> /// <returns></returns> private void writeExcelContent(Object o) { try { ExcelObj.Visible = false; ExcelDescriptions a = new ExcelDescriptions(); Hashtable templateXml = a.parseXml(templateDiscriptionFilePath); WookSheetWrite wStW = new WookSheetWrite(xSt); wStW.setWorksheet(o, templateXml); //xSt.Cells[2, 2] = "aa"; //xSt.Cells[25, 2] = "aa"; //xRng = xSt.get_Range("B2", missing); //xRng.Value2 = "aa"; //xRng = xSt.get_Range(xSt.Cells[2, 2], xSt.Cells[2, 2]); //xRng.HorizontalAlignment = -4108; //xRng.Borders.LineStyle = 1; //如果用到 xRng = xSt.get_Range(xSt.Cells[2, 2], xSt.Cells[2, 2]); 就必须关掉 //否则系统里就会有一个没关掉的 Excel.exe进程 //System.Runtime.InteropServices.Marshal.ReleaseComObject(xRng); //xSt.Cells[2, 5] = "bb"; //xRng = xSt.get_Range(xSt.Cells[2, 5], xSt.Cells[2, 5]); //xRng.HorizontalAlignment = -4108; //xRng.Borders.LineStyle = 1; //System.Runtime.InteropServices.Marshal.ReleaseComObject(xRng); xBk.Save(); } catch (Exception e) { close(); throw new Exception(e.Message + "操作临时Excel文件时出错"); } finally { close(); } } #endregion #region 根据传入的模版名设置绝对路径中的模板名 /// <summary> /// 根据传入的模版名设置绝对路径中的模板名 /// </summary> /// <param name="strTemplateName">模板名</param> /// <returns></returns> private string getTemplatePath(string templateName, string postfix) { if (string.IsNullOrEmpty(templateName)) { throw new Exception("模版文件名为空"); } //从配置文件中读取,Excel模版文件存放位置 string templateFolderPath = System.Configuration.ConfigurationSettings.AppSettings["ExcelTemplatePath"]; if (string.IsNullOrEmpty(templateFolderPath)) { throw new Exception("没有找到模版文件夹路径"); } return templateFolderPath + templateName + postfix; } #endregion #region 生成临时文件的文件名 /// <summary> /// 设置保存到临时文件中的文件名。 /// 带路径的。 /// 这是一个线程安全的方法。 /// </summary> /// <returns>临时文件名</returns> [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)] private string getTempFileName() { string tempFilePath = System.Configuration.ConfigurationSettings.AppSettings["ExcelTempFilePath"]; String strFile = System.DateTime.Now.ToFileTimeUtc().ToString() + ".xls";//创建一个临时文件名 if (string.IsNullOrEmpty(tempFilePath)) { throw new Exception("没有找到临时文件路径"); } return tempFilePath + strFile; } #endregion #region 下载文件 /// <summary> /// /// 在网络上 传一个文件 /// /// </summary> /// <param name="srcFileName">要传输的文件的名字</param> /// /// <param name="newFileName">新的文件的名字</param> public void sendExcelFile(string srcFileName, string newFileName) { if (!string.IsNullOrEmpty(srcFileName)) { //这里应该判断srcFileName 是否是全路径名 //System.IO.FileInfo file = new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + srcFileName); System.IO.FileInfo file = new System.IO.FileInfo(srcFileName); if (file.Exists) { string FileName = newFileName; string ExtendName = file.Name; if (ExtendName.Length > 4) { int i = ExtendName.LastIndexOf("."); ExtendName = ExtendName.Substring(i); FileName += ExtendName; } //Response.AppendHeader(@"Content-Disposition", @"attachment;filename=" + HttpUtility.UrlEncode("报表.xls",System.Text.Encoding.UTF8)); //这句话中的 attachment 改成 inline 就可以了 HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)); HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.Filter.Close(); HttpContext.Current.Response.WriteFile(file.FullName); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close(); //HttpContext.Current.Response.End(); } else { HttpContext.Current.Response.Write("对不起,文件" + AppDomain.CurrentDomain.BaseDirectory + srcFileName + "不存在"); } } } #endregion #region 关闭使用到的Excel进程 /// <summary> /// 关闭使用到的Excel进程 /// </summary> private void close() { //if的顺序不能变 if (ExcelObj != null) { if (xBk != null) { xBk.Close(null, null, null); } ExcelObj.Workbooks.Close(); ExcelObj.Quit(); } if (xRng != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xRng); xRng = null; } if (xSt != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xSt = null; } if (xBk != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk); xBk = null; } if (ExcelObj != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelObj); ExcelObj = null; } GC.Collect(); } #endregion }
配置文件
<appSettings > <add key="ExcelTempaltePath" value="F:/koide_sufen/ExcelTemplate/"/> <add key="ExcelTempFilePath" value="F:/koide_sufen/ExcelTempFile/"/> </appSettings>
调用
protected void Button1_Click(object sender, EventArgs e) { ExcelComManager a = new ExcelComManager(); a.createExcelAndSend("LOTO_SAMPLE.xls"); }
vs2008下测试通过
发表评论
-
C# 根据模版写Excel(Com)二
2008-11-18 12:57 2612《C# 根据模版写Excel(Com)一 》 里给出写Ex ... -
c# 通过反射 实例化类
2008-11-17 16:09 38341. 用Type 在 website 下 string ... -
GridView 嵌套 ObjectDataSource数据源绑定
2008-11-11 15:53 2758这个方法是目前我知道的最好的绑定嵌套的GridView方法了。 ... -
ajax gridview 嵌套
2008-11-10 17:15 2649ajax GridView 嵌套 弹出模态窗口 页面结 ... -
C# 线程同步
2008-10-31 10:57 1082原文地址 http://www.cnblogs.com/m ... -
让ajax 控件先执行服务端事件
2008-10-28 15:51 1327这里用的是 一个 ModalPopupExtender 控件 ... -
net 常用正则
2008-10-23 13:35 808从别人那里 copy过来的 http://www.cnbl ... -
gridview排序
2008-10-14 13:58 4394在GridView中可以通过两种方式来指定模板列的标题: ... -
gridview 学习
2008-10-08 15:04 1000一个对象A里 包含另一个对象B时 要在gridview里帮定 ...
相关推荐
C#操作EXCEL EXCEL类库 Excel模板处理 将DataTable数据写入Excel文件 C#数据写入EXCEL EXCEL样式管理帮助类库
C# 将数据导出到excel模板 自定义字段 excel表格可复杂 解压密码: www.shcodetool.cn
不是直接导出表格,而是导出像人事档案,请假条之类的表格形式,客户在EXCEL中自行定义导出的字段和式样。
C#创建EXCEl模板,程序自动创建EXCEL文件,并且根据需要自动填充单元格内容!输出EXCEL文件
C#版本Itextsharp根据模板导出pdf报表
C#按模板导出EXCEL的通用类,功能强大。包括Datatable和二维数组数据源,同时有分页的功能。(模板为空即生成新的文件)
原创. 我的水平不怎么样,多多指教,有跟好的方法希望交流.
C#读取Excel单元格内容并赋值给text一个简单demo C#读取Excel单元格内容并赋值给text一个简单demo C#读取Excel单元格内容并赋值给text一个简单demo
c#源码工具类型,主要用于EXCEL模板编辑,批量导入图片到EXCEL固定单元格,批量导入文字到EXCEL固定单元格,实现拖放图片显示,循环遍历子控件添加事件等资源,程序附加模板。可直接使用。扩展性强,下载后希望给个...
向 EXCEL 模板文件 插入 数据行,不破坏EXCEL模板原有的格式.
公司因业务需求,需要开发一个自动向供应商发送邮件的系统。难点是 邮件需附带一个Excel附件,此...根据供应商的不同而不同,相关部门已提供一个Excel内容模板。 用VS2010写了一个例子,解决了此问题。与大家分享!
NPOI基于模板导出到excel, 支持excel2007以上版本和2003以下版本
由于项目需求,需要根据现有的word模板,进行批量生成word报表,为了方便,选用的是 NPOI (NPOI是一个开源的C#读写Excel、WORD等微软OLE2组件文档的项目)来实现。本次用的版本是NPOI 2.3.0.0,版本不同写法会有...
ASP.NET/C# 使用NPOI 将数据导入到模板Excel里[源代码] . 支持xlsx,如果需要支持xls,请使用HSSFWorkbook
向固定模版的EXCEL文件中写入数据
C#winform,excel打印工具,以excel为基础模板,为winform系统打印功能提供支持,方便灵活,易于接受,分享大家。
主要的问题是EXCEL生成图的问题,因为系统的数据表很多个表(数据量也很大,经常上几十万行数据),而且分析字段也不固定,这意味着不好利用实现准备好的模板,c#打开模板把数据写到相应的地方,自动就生成图的方法...
通过已有的打印模板将form画面上输入的数据输出到excel中,类似水晶报表,但是相比较实现要快捷简单,易于操作,应用到的是excel.dll 值得使用
这是一个C#与excel做接口的模板,办公自动化软件的开发可在此基础上做二次开发使用。VS2010下做的模板。
C#打印EXCEL基本方法,资源为.cs文件。具体可参考博客内容。