将listView中查询到的部分数据写入到Excel模板中

工作上用到需要将listView中的部分数据写入到Excel模板中,在网上找,好多都是将整个listView写入到Excel中,所以记录一下吧

有借鉴的网上的方法,下面贴出源码:(里面有冗余的代码,大家若需要,按需修改哈)

用到的Excel模板是 国家知识产权局 专利缴费信息网上补充 支付方式为 支票的模板

http://fee.sipo.gov.cn/  选窗口缴费--》支票  下面的模板

//2017-02-10 swl 打印缴费清单 Excel 版本
		private void btPrintPayList2_Click(object sender, System.EventArgs e)
		{

		//下面是为了 先得到一共勾选了多少条,且总共包含多少count条的有效数据 listView的名字是lvNoPay
		/* 	int count1 = this.lvNoPay.CheckedItems.Count;  //一共勾选了多少条数据
			int count2 = count1;
			for(int i = 0 ; i < count1; i++ )   //进入勾选的每一条数据里面
			{
				if(this.lvNoPay.CheckedItems[count1 - 1].SubItems[17].Text != "0")  //如果滞纳金不为0 则 +1
				{
					count2++;
				}
				if(this.lvNoPay.CheckedItems[count1 - 1].SubItems[18].Text != "0")  //如果恢复费不为0 则 +1
				{
					count2++;
				}
			} */
			ToExcel();  //调用打印Excel的函数
		}


		public void ToExcel() //传入一共需要打印多少条数据
		{
			string excelTemplateDPath = "";
			
			//EXCEL模板默认服务器物理存放路径
			string tempEFilePath = @"D:A";
			
			//EXCEL临时文件保存服务器物理存放路径 
			string tempEFileXPath = @"D:A";
			

			//EXCEL临时文件保存服务器虚拟存放路径 
			Excel.Application app = null;

			//EXCEL对象
			Excel.Workbooks workbooks;
			//工作簿集合
			Excel.Workbook workbook;
			//当前工作簿
			Excel.Sheets sheets;
			//SHEET页集合
			Excel._Worksheet worksheet; 

			//excelTemplateDPath = tempEFilePath + "//a1.xls";
			excelTemplateDPath = tempEFilePath + "//国家申请或集成电路费用信息模板.xls";

		

			//验证EXCEL临时文件夹是否存在
			if (!File.Exists(tempEFilePath))
			{ 
				Directory.CreateDirectory(tempEFilePath); 
			} 

			//启动EXCEL进程 
			app = new Excel.Application();
//			if (app == null)
//			{
//				rp.IsError = true; 
//				rp.Message = "Excel进程启动出错,请确认是否引用EXCEL组件";
//				return rp;
//			}
			app.Visible = true;  //可见改为TRUE
			app.UserControl = true;
			app.DisplayAlerts = false;

			//加载读取模板
			workbooks = app.Workbooks;
			workbook = workbooks.Add(excelTemplateDPath);
			sheets = workbook.Worksheets; 
			worksheet = (Excel._Worksheet)sheets.get_Item(1);

			int count1 = this.lvNoPay.CheckedItems.Count;  //一共勾选了多少条数据

			int count2 = 0;  //打印的序号
			int tempExcelHH = 1;	//Excel的行号
			//填充数据
			for (int i = 2; i <= count1 + 1; i++)  //一共勾选了多少条 作为循环次数
			{ 
				//1. 先把本条的年费写到Excel上   2.再把滞纳金  3.最后是恢复费

				string tempSQH = this.lvNoPay.CheckedItems[i - 2].SubItems[2].Text;

				//如果申请号含有小数点 则去除小数点 
				int tempIndex = tempSQH.IndexOf(".");
				if(tempIndex > -1)   //包含小数点
				{
					string[] sArray2 =  tempSQH.Split('.');
					tempSQH = sArray2[0] + sArray2[1];
				}
				string tempYear = this.lvNoPay.CheckedItems[i - 2].SubItems[1].Text;     //年数


				string tempType = "";   //需要的类型
				string tempTypeNum=tempSQH.Substring(4,1);   //1和8发明 2实用新型  3外观设计
				if(tempTypeNum == "1" || tempTypeNum == "8")
				{
					tempType = "发明专利第"+tempYear+"年年费";
				}

				if(tempTypeNum == "2")
				{
					tempType = "实用新型专利第"+tempYear+"年年费";
				}

				if(tempTypeNum == "3")
				{
					tempType = "外观设计专利第"+tempYear+"年年费";
				}
				//string tempType = "发明专利第"+tempYear+"年年费";			//转换为需要的类型

				string tempFee = this.lvNoPay.CheckedItems[i - 2].SubItems[7].Text;   //原始显示的费用 900.00¥,900¥
				string[] sArray=tempFee.Split('¥') ;
				tempFee= sArray[0];  //得到费用 900.00

				count2++;

				string tempXH = count2.ToString();

				tempExcelHH++;	//Excel的行号 +1
				//下面是勾选的一行的年费
				worksheet.Cells[tempExcelHH, 1] = tempXH;   //序号
				worksheet.Cells[tempExcelHH, 2] = tempSQH;   //申请号
				worksheet.Cells[tempExcelHH, 3] = tempType;			//费用种类
				worksheet.Cells[tempExcelHH, 4] = tempFee;			//费用金额


				//如果勾选的一行的滞纳金不为0,则写入Excel
				string tempZNJ = this.lvNoPay.CheckedItems[i - 2].SubItems[17].Text;
				if(tempZNJ != "0")
				{
					count2++;  //序号+1
					tempXH = count2.ToString();
					tempExcelHH++;   //Excel换下一行
					tempType = "发明专利年费滞纳金";
					worksheet.Cells[tempExcelHH, 1] = tempXH;   //序号
					worksheet.Cells[tempExcelHH, 2] = tempSQH;   //申请号
					worksheet.Cells[tempExcelHH, 3] = tempType;	//费用种类
					worksheet.Cells[tempExcelHH, 4] = tempZNJ;
				}

				//如果勾选的一行的恢复费不为0,则写入Excel
				string tempHFF = this.lvNoPay.CheckedItems[i - 2].SubItems[18].Text;
				if(tempHFF != "0")
				{
					count2++;	//序号+1
					tempXH = count2.ToString();
					tempExcelHH++;   //Excel换下一行
					tempType = "恢复权利请求费";
					worksheet.Cells[tempExcelHH, 1] = tempXH;   //序号
					worksheet.Cells[tempExcelHH, 2] = tempSQH;   //申请号
					worksheet.Cells[tempExcelHH, 3] = tempType;	//费用种类
					worksheet.Cells[tempExcelHH, 4] = tempHFF;   //恢复费现在固定为1000
				}

			}

			//设置自动列宽
			try 
			{ 
				string tempFileName = "TaskList" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; 
				tempEFilePath += "//" + tempFileName; 
				tempEFileXPath += "/" + tempFileName;
				//worksheet.SaveAs(tempEFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing); 
				worksheet.SaveAs(tempEFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing,Type.Missing, Type.Missing, Type.Missing); 
				//DeleteTempFiles(tempEFilePath); 
				//清除临时文件
			} 

			catch (Exception ex) 
			{ 

			}
			finally
			{ 
				GC.Collect(); 
			}
		}

  

原文地址:https://www.cnblogs.com/surfacebook007/p/6406498.html