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的更多相关文章

  1. xls 和 xml 数据 排序 绑定 -原创

    xls 和 xml 排序 xml: <?xml version="1.0" encoding="UTF-8"?> <?xml-styleshe ...

  2. Report_客制化以PLSQL输出XLS标记实现Excel报表(案例)

    2015-02-12 Created By BaoXinjian

  3. Excel和XML文件导入

    using System;using System.Collections;using System.Collections.Generic;using System.Configuration;us ...

  4. xml与Excel转换

    使用Python将如下xml格式转换为Excel格式: xml转为xls格式文件: xml格式如下: <?xml version="1.0" encoding="U ...

  5. PHP导出XML格式的EXCEL

    <?php function Export(){ set_time_limit(0); ob_start(); $biz = new ZaikuBiz(); $biz->setSearch ...

  6. 【原创】开源BI领袖-SpagoBI5.X最详细的中文版介绍

    SpagoBI是唯一100%的开源商业智能套件由 Engineering Group的SpagoBI实验室(www.eng.it)开发和管理.它提供了强大的分析能力,从传统的报表和图表功能到自助分析. ...

  7. 30 个 PHP 的 Excel 处理类

    下面的 PHP Excel 处理类中,包含 Excel 读写.导入导出等相关的类,列表如下: PHP Excel Reader classes 1. Read Excel Spreadsheets u ...

  8. Python应用与实践【转】

    转自:http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html 目录 1.      Python是什么? 1.1.      Pyt ...

  9. 29 个 PHP 的 Excel 处理类

    下面的 PHP Excel 处理类中,包含 Excel 读写.导入导出等相关的类,列表如下: PHP Excel Reader classes 1. Read Excel Spreadsheets u ...

随机推荐

  1. .Net Core部署IIS

    1.环境安装 https://dotnet.microsoft.com/download/dotnet-core 下载Runtime & Hosting Bundle包安装,iis模块会出现支 ...

  2. 自己写的一些公共js方法

    /* 说明文件:这里用的都是es6的语法 导入导出,拿vue举个栗子,你只需要在用到的地方,按需要导入就行了,然后在mounted中直接可以拿来用 比如下面的手机****方法,在需要用到的地方impo ...

  3. POJ 1655 求树的重心

    POJ 1655 [题目链接]POJ 1655 [题目类型]求树的重心 &题意: 定义平衡数为去掉一个点其最大子树的结点个数,求给定树的最小平衡数和对应要删的点.其实就是求树的重心,找到一个点 ...

  4. #WEB安全基础 : HTTP协议 | 0x9 GET和POST请求以及请求URI的方式

    请求URI的方式 1.URI为完整的请求URI GET http://hackr.jp/index.htm HTTP/1.1 2.在首部字段Host中写明域名或IP地址 GET/index.htm H ...

  5. servlet中如何实现通过Spring实现对象的注入

    @WebServlet("/BaseServlet")public class BaseServlet extends HttpServlet { private static f ...

  6. Delphi获取本机所有的IP

    安装Indy uses  IdStackWindows; var Isw:TIdStackWindows; slist:TStringList; begin Isw:=TIdStackWindows. ...

  7. vue 定义全局函数,监听android返回键事件

    vue 定义全局函数,监听android返回键事件 方法一:main.js 注入(1)在main.js中写入函数Vue.prototype.changeData = function (){ aler ...

  8. jQuery之Deferred对象P2

    转自 https://www.cnblogs.com/losesea/p/4415676.html deferred对象是jQuery对Promises接口的实现.它是非同步操作的通用接口,可以被看作 ...

  9. 初步了解,vue的转发机制(proxyTable)

    vue的转发机制(proxyTable),proxyTable代理功能可以实现转发机制,需要修改config里面修改index.js文件 修改index.vue中请求的代码(该功能是webpack-s ...

  10. 虚拟蜜罐honeyd安装使用

    转https://blog.csdn.net/jack237/article/details/6828771