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; //Worksheet = Microsoft.Office.Interop.Excel.Worksheet (Microsoft.Office.Interop.Excel.Worksheet) sheet.get_Item (. 1); // Get sheet (1) for the first Sheet // Double usedRows = app.WorksheetFunction. COUNTA (worksheet.Columns [. 3]); // number of rows of the third column // String usedRows.ToString NUM = (); // Object [,] twoDoubleList worksheet.Range = [ "A1: the AH" NUM +]. value2; // get the array a using (the DataSet ds = new new the DataSet ()) { // determine Excel file is the 2003 version or the 2007 version 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 = newThe XmlTextWriter (Stream, Encoding.Unicode); // . WriteXml method of writing a file xmlDS.WriteXml (Writer); int COUNT = ( int ) Stream.length; byte [] = ARR new new byte [COUNT]; stream.Seek ( 0 , SeekOrigin.Begin); Stream.Read (ARR, 0 , COUNT); // returns the Unicode encoded text UnicodeEncoding, UTF = new new UnicodeEncoding, (); the StreamWriter SW = new new the 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(); } } } }
This is the first generation version of the conversion tool.
Download: https: //pan.baidu.com/s/1KCA5E367g26GIvhJNVJKxw