win32 导入excel文档模板,写入数据

首先,将excel文档另存为xml,相关方法,上一篇博文已经做过介绍,此处不再累述。

打开模板xml:

	xmlDocPtr doc;
	xmlNodePtr cur;
	xmlNodePtr parent;
	//doc = xmlParseFile("sheet1.xml");
	doc = xmlReadFile("sheet1.xml", NULL,XML_PARSE_NOBLANKS);
	if (doc == NULL ) {
		return;
	}
	cur = xmlDocGetRootElement(doc);
	if (cur == NULL) {
		xmlFreeDoc(doc);
		return;
	}
	if (xmlStrcmp(cur->name, (const xmlChar *) "Workbook")) {
		xmlFreeDoc(doc);
		return ;
	}
	cur = cur->xmlChildrenNode;
	while (cur != NULL) {
		if ((!xmlStrcmp(cur->name, (const xmlChar *)"Worksheet"))){
			//parseStory (doc, cur);
			break;
		}
		cur = cur->next;
	}

	cur = cur->children;
	parent = cur;
	cur = cur->children;
	if(!cur)
	{
		xmlFreeDoc(doc);
		return;
	}

	while(cur)
	{
		if (!xmlStrcmp(cur->name, (const xmlChar*)"Row"))
		{
			break;
		}
		cur = cur->next;
	}

	int i;
	for(i = 0; i < 3 && cur; i++)
	{
		cur = cur->next;
	}
        // 删除所有的列,只取xml文档第一部分、第二部分,见上一篇博文,当然也可以保留一行,作为所有行的数据模板,读取这行的相关格式ID
	DeleteRowElements(doc, cur);
	int j = 0;
	for(j = 0;j < m_attendance.m_employee_num;j++)
	{   
		xmlNodePtr row;
		employee e;
		e = m_attendance.m_employee_list[j];
                //新建一行
		row = xmlNewChild(parent, NULL,  (const xmlChar*)"Row",NULL);
		xmlNewProp(row,  (const xmlChar*)"ss:StyleID", (const xmlChar*) "s50");
		xmlNewProp(row, (const xmlChar*) "ss:Height", (const xmlChar*) "20.1");
                //对每个单元格插入数据,这里假设有20列
		for(i = 0; i< 20; i++)
		{   
			xmlNodePtr cell;
			xmlNodePtr data;
			char buf[64];
			memset(&buf[0], 0, 64);
			switch(i)
			{
			case 0:
 				//写入属性时,加入相应的名空间
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s64");
				data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",e.name);
                xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"String");
				break;
			case 1:
				//m_attendance.AddEmployeeType(content);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s65");
				data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",e.employee_type);
				xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"String");
				break;
			case 2:
				//m_attendance.AddEmployeeTotalWorkDays(days);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				sprintf(&buf[0],"%d",e.total_work_days);
				data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
				xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				break;
			case 3:
				//m_attendance.AddEmployeeTotalAttendDays(days);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				sprintf(&buf[0],"%d",e.total_attend_days);
				data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
				xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				break;
			case 4:
				//m_attendance.AddEmployeeAnnualHolidayDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.Annual_holiday_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.Annual_holiday_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 5:
				//m_attendance.AddOfficialBusinessOutCityDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.official_business_out_city_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.official_business_out_city_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 6:
				//m_attendance.AddOfficialBusinessInCityDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.official_business_in_city_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.official_business_in_city_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 7:
				//m_attendance.AddExchangeHolidayDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.exchange_holiday_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.exchange_holiday_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 8:
				//m_attendance.AddExtraWorkHours(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.extra_work_hours != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.extra_work_hours);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 9:
				//m_attendance.AddPaidSickHolidayDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.paid_sick_holiday_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.paid_sick_holiday_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 10:
				//m_attendance.AddOfficialInjuryDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.official_injury_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.official_injury_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 11:
				//m_attendance.AddNopaidSickHolidayDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.no_paid_sick_holiday_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.no_paid_sick_holiday_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 12:
				//m_attendance.AddPrivateBusinessDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell,(const xmlChar*)"ss:StyleID", (const xmlChar*)"s79");
				if(e.private_business_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.private_business_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 13:
				//m_attendance.AddProbationClassDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s79");
				if(e.probation_class_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.probation_class_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 14:
				//m_attendance.AddFunaralLeaveDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.funeral_leave_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.funeral_leave_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 15:
				//m_attendance.AddPregnancyLeaveDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.pregnancy_leave_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.pregnancy_leave_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 16:
				//m_attendance.AddAccompanyPregnancyLeaveDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.accompany_pregnancy_leave_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.accompany_pregnancy_leave_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 17:
				//m_attendance.AddMaritalLeaveDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				if(e.marital_leave_days != 0.0)
				{   
					sprintf(&buf[0], "%.3f",e.marital_leave_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				}
				break;
			case 18:
				//m_attendance.AddEmployeeTotalActualTotalAttendDays(daysf);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s66");
				//if(e.actual_total_attend_days != 0.0)
				//{   
					sprintf(&buf[0], "%.3f",e.actual_total_attend_days);
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)&buf[0]);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"Number");
				//}
				break;
			case 19:
				//m_attendance.AddRemark(content);
				cell = xmlNewChild(row,NULL,(const xmlChar*)"Cell",NULL);
				xmlNewProp(cell, (const xmlChar*)"ss:StyleID", (const xmlChar*)"s87");
				if(e.remark)
				{
					data = xmlNewChild(cell,NULL,(const xmlChar*)"Data",(const xmlChar*)e.remark);
					xmlNewProp(data, (const xmlChar*)"ss:Type", (const xmlChar*)"String");
				}
				break;
			default:
				break;
			}	
		}
	}
 
       //保存文件,释放内存
	xmlSaveFormatFile ("r.xml", doc, 1);
	xmlFreeDoc(doc);



一个excel文档就生成了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值