C#实现Excel转成Json

由于在游戏开发过程中很多数据都来源于本地json文件,但是策划给的都是Excel文件,在网上找了一下没有Excel转成Json的小工具,想想还是自己动动手吧,自己动手丰衣足食,开始

这里C#对Json的操作呢我使用Newtonsoft.Json.dll,这个大家可以到这里下载

http://json.codeplex.com  用法请参考http://www.sulianqi.cn/Article.aspx?id=ART2013070200002

 

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 System.Data.OleDb;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.IO;

namespace ToJson
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.ShowDialog();
                textBox1.Text = openFileDialog1.FileName;
                comboBox1.DataSource = GetExcelSheets(openFileDialog1.FileName);
                comboBox1.DisplayMember = "TABLE_NAME";
                comboBox1.ValueMember = "TABLE_NAME";
                InitData();
                button2.Enabled = true;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
       
        public  DataSet GetExcelToDataTableBySheet(string FileFullPath, string SheetName)
        {
            string strConn = GetConStr(FileFullPath);
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", SheetName), conn); 
            odda.Fill(ds, SheetName);
            conn.Close();
            return ds;
        }
        public  DataTable GetExcelSheets(string FileFullPath)
        {
            string strConn = GetConStr(FileFullPath);
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            // 得到包含数据架构的数据表
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            conn.Close();
            return dt;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string result = JsonConvert.SerializeObject(dt, new DataTableConverter());
            StreamWriter sw = new StreamWriter("json.json", false, Encoding.UTF8);
            sw.Write(result);
            sw.Close();
            sw.Dispose();
            System.Diagnostics.Process.Start(Application.StartupPath);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitData();
        }
        private void InitData()
        {
            DataSet ds = GetExcelToDataTableBySheet(textBox1.Text, comboBox1.Text);
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt;
        }
        public string GetConStr(string FileFullPath)
        {
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'"; //此連接可以操作.xls與.xlsx文件HDR=YES第一行为列名
            if (checkBox1.Checked)
            {
                strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'"; //此連接只能操作Excel2007之前(.xls)文件
            }
            return strConn;
        }
    }
}

 

 

 

本示例源码:

http://pan.baidu.com/share/link?shareid=4231425908&uk=1714884264