|
楼主 |
发表于 2015-12-19 20:16
|
显示全部楼层
本帖最后由 VBA万岁 于 2015-12-20 12:31 编辑
问题八(一)测试成功的主要代码:
- private void button1_Click(object sender, EventArgs e)
- {
- object o = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
- Microsoft.Office.Interop.Excel._Application app = o as Microsoft.Office.Interop.Excel._Application;
- Microsoft.Office.Interop.Excel.Workbook destbook = app.ActiveWorkbook;//得到当前活动的excel文档
- Microsoft.Office.Interop.Excel.Worksheet excel = (Microsoft.Office.Interop.Excel.Worksheet)destbook.ActiveSheet;//得到当前Sheet
- //以下被注释掉的代码可用于在新增的工作簿工作表中导入省市区数据,以上代码用于在当前活动工作表中导入省市区数据
- //Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
- //excel.Application.Workbooks.Add(true);
- //excel.AlertBeforeOverwriting = false;
- excel.Cells[1, 1] = "省";
- excel.Cells[1, 2] = "市";
- excel.Cells[1, 3] = "区";
- int n=1;
- XmlNode provinces = doc.SelectSingleNode("/ProvinceCity");
- foreach (XmlNode province in provinces.ChildNodes)
- {
- XmlNodeList cities = doc.SelectNodes(string.Format("/ProvinceCity/{0}/City", province.Name));
- foreach (XmlNode city in cities)
- {
- XmlNodeList CityAreas = doc.SelectNodes(string.Format("/ProvinceCity/{0}/City[@Name='{1}']/CityArea",province.Name,city.Attributes["Name"].Value));
- foreach (XmlNode area in CityAreas)
- {
- n++;
- excel.Cells[n, 1] = province.Name;
- excel.Cells[n, 2] = city.Attributes["Name"].Value;
- excel.Cells[n, 3]=area.Attributes["Name"].Value;
- }
- }
- }
- //excel.Visible = true;
- //excel.ActiveWorkbook.SaveAs(System.Windows.Forms.Application.StartupPath + "\\省市区三级联动.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, true);
- //excel.Quit();
- //excel = null;
- }
复制代码 |
|