xls到xml
protected void btn_ok_Click(object sender, EventArgs e)
{
string x = txtpath.Text;
DataSet ds = GetConnect(x);
DataTable xDataTable = ds.Tables[0]; string xFile = Server.MapPath("~/") + "action.xml";
if (File.Exists(xFile))
File.Delete(xFile);
//建立Xml的定义声明
XmlDocument xmlDoc = new XmlDocument();
try
{ XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null); xmlDoc.AppendChild(dec); //创建根节点
XmlElement root = xmlDoc.CreateElement("data");
xmlDoc.AppendChild(root);
for (int i = 0; i < xDataTable.Rows.Count; i++)
{
var str=xDataTable.Rows[i][1].ToString();
if(str=="")
{
break;
} if (i < 5)
{
var sdate = "2015年" + (xDataTable.Rows[i][3] + "").Substring(0, 5);
var edate = "2015年8月" + ((xDataTable.Rows[i][3] + "").Substring(6, 2));
//TimeSpan days=Convert.ToDateTime(edate) - Convert.ToDateTime(sdate);
//var n = int.Parse(days.ToString());
for (int r = 0; r <= 6; r++)
{ XmlNode item = xmlDoc.CreateElement("item");
XmlElement title = xmlDoc.CreateElement("Title");
title.InnerText = xDataTable.Rows[i][1] + "";
item.AppendChild(title);
XmlAttribute xmldate = xmlDoc.CreateAttribute("date");
xmldate.Value = "8月"+(Convert.ToDateTime(sdate).AddDays(r).ToString()).Substring(7,3)+"日";
title.Attributes.Append(xmldate);
XmlAttribute xmlchangguan = xmlDoc.CreateAttribute("changguan");
xmlchangguan.Value = xDataTable.Rows[i][4] + "";
title.Attributes.Append(xmlchangguan);
XmlAttribute xmlisImportion = xmlDoc.CreateAttribute("isImportion");
xmlisImportion.Value = "1";
title.Attributes.Append(xmlisImportion); XmlElement jiaBin = xmlDoc.CreateElement("jiaBin");
jiaBin.InnerText = xDataTable.Rows[i][2] + "";
item.AppendChild(jiaBin); XmlElement time = xmlDoc.CreateElement("time");
time.InnerText = "9:00-21:00";
item.AppendChild(time); XmlElement address = xmlDoc.CreateElement("address");
address.InnerText = xDataTable.Rows[i][4] + "";
item.AppendChild(address); XmlElement zhuBanDanWei = xmlDoc.CreateElement("zhuBanDanWei");
zhuBanDanWei.InnerText = xDataTable.Rows[i][5] + "";
item.AppendChild(zhuBanDanWei); root.AppendChild(item);
}
}
else
{
XmlNode item = xmlDoc.CreateElement("item");
XmlElement title = xmlDoc.CreateElement("Title");
title.InnerText = xDataTable.Rows[i][1] + "";
item.AppendChild(title);
XmlAttribute xmldate = xmlDoc.CreateAttribute("date");
xmldate.Value = (xDataTable.Rows[i][3] + "").Substring(0,5);
title.Attributes.Append(xmldate);
XmlAttribute xmlchangguan = xmlDoc.CreateAttribute("changguan");
xmlchangguan.Value = xDataTable.Rows[i][4] + "";
title.Attributes.Append(xmlchangguan);
XmlAttribute xmlisImportion = xmlDoc.CreateAttribute("isImportion");
xmlisImportion.Value = "1";
title.Attributes.Append(xmlisImportion); XmlElement jiaBin = xmlDoc.CreateElement("jiaBin");
jiaBin.InnerText = xDataTable.Rows[i][2] + "";
item.AppendChild(jiaBin); XmlElement time = xmlDoc.CreateElement("time");
int index = (xDataTable.Rows[i][3] + "").IndexOf("日");
time.InnerText = (xDataTable.Rows[i][3] + "").Substring(index + 1);
item.AppendChild(time); XmlElement address = xmlDoc.CreateElement("address");
address.InnerText = xDataTable.Rows[i][4] + "";
item.AppendChild(address); XmlElement zhuBanDanWei = xmlDoc.CreateElement("zhuBanDanWei");
zhuBanDanWei.InnerText = xDataTable.Rows[i][5] + "";
item.AppendChild(zhuBanDanWei); root.AppendChild(item);
}
} xmlDoc.Save(xFile);
lblshow.InnerText = "成功";
}
catch (Exception ex)
{
lblshow.InnerText = "失败" + ex.Message + ex.TargetSite;
} }
/// <summary>
/// 导出EXCEL表中的数据到 myDataSet
/// </summary>
public static DataSet GetConnect(string FilePath)
{
DataSet myDataSet = new DataSet();
string strCon = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + FilePath + "; Extended Properties=\"Excel 8.0; HDR=YES; IMEX=1;\"";
if (Path.GetExtension(FilePath).ToLower() == ".xlsx")
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
OleDbConnection myConn = new OleDbConnection(strCon);
//条件查询EXCEL表
string strCom = " SELECT * FROM [Sheet1$] ";
myConn.Open();
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
//创建一个 DataSet对象 //得到自己的DataSet对象
myCommand.Fill(myDataSet, "[Sheet1$]");
//关闭此数据链接
myConn.Close();
return myDataSet;
}
xls到xml的更多相关文章
- xls 和 xml 数据 排序 绑定 -原创
xls 和 xml 排序 xml: <?xml version="1.0" encoding="UTF-8"?> <?xml-styleshe ...
- Report_客制化以PLSQL输出XLS标记实现Excel报表(案例)
2015-02-12 Created By BaoXinjian
- Excel和XML文件导入
using System;using System.Collections;using System.Collections.Generic;using System.Configuration;us ...
- xml与Excel转换
使用Python将如下xml格式转换为Excel格式: xml转为xls格式文件: xml格式如下: <?xml version="1.0" encoding="U ...
- PHP导出XML格式的EXCEL
<?php function Export(){ set_time_limit(0); ob_start(); $biz = new ZaikuBiz(); $biz->setSearch ...
- 【原创】开源BI领袖-SpagoBI5.X最详细的中文版介绍
SpagoBI是唯一100%的开源商业智能套件由 Engineering Group的SpagoBI实验室(www.eng.it)开发和管理.它提供了强大的分析能力,从传统的报表和图表功能到自助分析. ...
- 30 个 PHP 的 Excel 处理类
下面的 PHP Excel 处理类中,包含 Excel 读写.导入导出等相关的类,列表如下: PHP Excel Reader classes 1. Read Excel Spreadsheets u ...
- Python应用与实践【转】
转自:http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html 目录 1. Python是什么? 1.1. Pyt ...
- 29 个 PHP 的 Excel 处理类
下面的 PHP Excel 处理类中,包含 Excel 读写.导入导出等相关的类,列表如下: PHP Excel Reader classes 1. Read Excel Spreadsheets u ...
随机推荐
- How to Preloading content with rel preload
The preload value of the <link> element's rel attribute allows you to write declarative fetch ...
- 67.web--手机端兼容性问题
H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-width,initia ...
- PHP----------linux下安装opcache.
1.首先查看是否安装了opcache扩展,使用php -m 命令查看安装的扩展(没有添加环境变量就使用:/usr/local/php/bin/php -m). 添加opcache扩展.(我自己封装的s ...
- Python学习笔记(Ⅱ)——循环/选择/函数
一.循环结构 python中提供了for循环和while循环两种操作,没有do……while语句. 1.for循环: 与其他语言中for循环的常见的写法如for (int i=0;i<10;i+ ...
- zabbix监控实战<2>----zabbix-server的安装与部署
第一章 zabbix-server的安装与部署 1.1 环境部署 eth0 eth1 master 10.0.0.71 ...
- 装饰者模式 Decorator
项目:咖啡计费系统 背景:现有系统中有一个抽象类Beverage,有2个抽象方法GetDescription和Cost. namespace DecoratorPattern { /// <su ...
- 解决c# progressBar更新出现界面假死
最近一个项目需求中的一个功能是需要用progressBar反映处理文件的进度. 研究了Invoke和BeginInvoke方法. Control.Invoke 方法 (Delegate) :在拥有此控 ...
- Centos7 安装tomcat
- calss 类
class Role(object): n = 123 # 类变量,像__init__中life_value,money默认参数用类变量,如果多个类的实例那就节省了内存,类变量只在类中存一份,实例中不 ...
- BP神经网络(原理及MATLAB实现)
人工神经网络概述: 人工神经元模型: 神经网络的分类: 按照连接方式,可以分为:前向神经网络 vs. 反馈(递归)神经网络: 按照学习方式,可以分为:有导师学习神经网络 vs. 无导师学习神经网络: ...