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

    xiaoxiao2021-03-25  104

    首先,将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文档就生成了。

    转载请注明原文地址: https://ju.6miu.com/read-20190.html

    最新回复(0)