Excel转换成xml文件

namespace ExcelToXml
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Program program = new Program();
            DataSet dataSet= program.getData();
            Program.ConvertDataSetToXMLFile(dataSet,"D:\\"+dataSet.DataSetName+".xml");
        }
        public DataSet getData()
        {
            //打开文件
            OpenFileDialog file = new OpenFileDialog();
            file.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";
            file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            file.Multiselect = false;
            if (file.ShowDialog() == DialogResult.Cancel)
                return null;
            //判断文件后缀
            var path = file.FileName;

            string name = Path.GetFileNameWithoutExtension(path);
            string fileSuffix = System.IO.Path.GetExtension(path);
            if (string.IsNullOrEmpty(fileSuffix))
                return null;

            ////加载Excel
            //Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();  //获取权限
            //Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks;
            //Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(path);
            //Microsoft.Office.Interop.Excel.Sheets sheet = workbook.Sheets;
            //Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheet.get_Item(1);//获取sheet  (1)为第一个sheet
            //double usedRows = app.WorksheetFunction.CountA(worksheet.Columns[3]); //第3列的行数
            //string num = usedRows.ToString();

            //object[,] twoDoubleList = worksheet.Range["A1:AH" + num].Value2; //获取数组

            using (DataSet ds = new DataSet())
            {

                //判断Excel文件是2003版本还是2007版本
                string connString = ""; //server=.;database=ExcelToXml;integrated security=SSPI
                if (fileSuffix == ".xls")
                    connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
                else
                    connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
                //读取文件
                string sql_select = " SELECT * FROM [Sheet1$]";
                using (OleDbConnection conn = new OleDbConnection(connString))
                using (OleDbDataAdapter cmd = new OleDbDataAdapter(sql_select, conn))
                {
                    conn.Open();
                    cmd.Fill(ds);
                    ds.DataSetName = name;
                }
                if (ds == null || ds.Tables.Count <= 0) return null;
                return ds;
            }
        }

        public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;
            try
            {
                stream = new MemoryStream();
                //从stream装载到XmlTextReader
                writer = new XmlTextWriter(stream, Encoding.Unicode);
                //用WriteXml方法写入文件.
                xmlDS.WriteXml(writer);
                int count = (int)stream.Length;
                byte[] arr = new byte[count];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(arr, 0, count);
                //返回Unicode编码的文本
                UnicodeEncoding utf = new UnicodeEncoding();
                StreamWriter sw = new StreamWriter(xmlFile);
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sw.WriteLine(utf.GetString(arr).Trim());
                sw.Close();
            }
            catch (System.Exception ex)
            {
                //throw ex;
                MessageBox.Show(ex.Message);
                //Console.WriteLine(ex.Message);
                //Console.ReadLine();
            }
            finally
            {
                if (writer != null) writer.Close();
            }
        }

    }
}

这个是转换工具的初代版本。

工具下载:https://pan.baidu.com/s/1KCA5E367g26GIvhJNVJKxw

原文地址:https://www.cnblogs.com/qmz-blog/p/11511125.html

时间: 2025-01-18 11:03:38

Excel转换成xml文件的相关文章

c#程序将excel文件转换成xml文件

要程序你自己去组装去,我只写两个部分,一个是读Excel的部分,然后是写入到xml的1) 从指定的excel读出信息string strConn="provider=Microsoft.Jet.OLEDB.4.0;data source=你的Excel文件.xls;Extended Properties=Excel 8.0;";DataSet ds=new DataSet();System.Data.OleDb.OleDbConnection oleConn=new System.Da

【练习题】编程把INI文件转换成XML文件

;Configuration of http [http] domain=www.mysite.com port=8080 cgihome=/cgi-bin ;Configuration of db [database] server = mysql user = myname password = toopendatabase 一个配置文件由若干个Section组成,由[]括号括起来的是Section名.每个Section下面有若干个key = value形式的键值对( Key-value P

用Java将Excel的xls和xlsx文件转换成csv文件的方法, XLS2CSV, XLSX2CSV

利用poi将excel文件后缀为.xls .xlsx的文件转换成txt/csv文本文件 首先,引入所需的jar包: <dependencies> 2 <dependency> 3 <groupId>net.sf.opencsv</groupId> 4 <artifactId>opencsv</artifactId> 5 <version>2.1</version> 6 </dependency> 7

.net excel 转换成datatable,创建文件夹

protected void Button9_Click(object sender, EventArgs e) { string path = ""; path = FileUpload3.PostedFile.FileName; if (path == "") { string jss = "<script language='javascript' type='text/javascript'> alert('先选择文件')</sc

C#.net word excel powerpoint (ppt) 转换成 pdf 文件

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word;using Excel = Microsoft.Office

如何把excel文件转换成pdf文件

我们经常需要将excel文档转换成pdf文件,这是一种最简单.最方便的方法,视频播放地址:http://v.youku.com/v_show/id_XODM2MTk0NzI0.html

android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件

前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xstream 二: 把对象转换成xml android XMl 解析神奇xstream 三: 把复杂对象转换成 xml android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象 1.建立JavaBeen package com.android10; public class

excel表格转换成pdf文件的方法有哪些?

excel表格中我们可以将一些数据转换成pdf文件的格式,有一些重要的内容时,我们可以将excel表格进行在线转换,怎么将excel转换成pdf格式的呢?下面就让小编给大家简单介绍一下. 步骤一:我们可以直接进入到这样一个迅捷PDF在线转换器,在线网站的首页面中去: 步骤二:在文档转换中找到excel转pdf,然后点击进入: 步骤三:视频文件可以直接点击界面进行文件的选择了,将excel表格上传到界面中去: 步骤四:文件上传完成之后就可以进行文件的在线转换了,转换是需要一定时间的: 步骤五:等文

java将office文档pdf文档转换成swf文件在线预览

java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org   openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文本文档),impress(演示文稿),Calc(电子表格),Draw(绘图),Math(公式),base(数据库) 笔者下载的是openoffice.org 3.3.0.下载完直接安装即可.      但是,我们还需要启动openof