Cách Chuyển File Excel Sang Xml Trực Tuyến Miễn Phí, Convert Xls To Xml Online, Free

Xin chào các bạn, bài viết hôm nay, mình sẽ hướng dẫn các bạn cách chuyển đổi file Excel sang định dạng XML trong lập trình C#.

Đang xem: Cách chuyển file excel sang xml

– Cách chuyển đổi:

+ Đầu tiên đọc dữ liệu file Excel lên DataTable.

Xem thêm: Tính Cách Trẻ 3 Tuổi Đến 5 Tuổi Đa Dạng Đến Không Ngờ, Tâm Lý Trẻ 3

+ Foreach trên DataTable để chuyển Column trong table thành Node XML.

Xem thêm: công thức giải bất phương trình bậc 2

*

Thiết kế giao diện ứng dụng:

*

Hình ảnh bên dưới là kết quả chuyển đổi từ Excel sang XML

*

1. Đầu tiên các bạn cần tạo một class ConvertXml.cs với source code C# bên dưới:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.OleDb;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;using System.Xml.Linq;using System.IO;namespace ConvertXmlApp{ class ConvertXml { // Creating Xml File With Custome FileName public bool CreateXltoXML(string XmlFile, string XlFile, string RowName) { bool IsCreated = false; try { DataTable dt = GetTableDataXl(XlFile); XmlTextWriter writer = new XmlTextWriter(XmlFile, System.Text.Encoding.UTF8); writer.WriteStartDocument(true); writer.Formatting = Formatting.Indented; writer.Indentation = 2; writer.WriteStartElement(“tbl_” + RowName); List ColumnNames = dt.Columns.Cast().ToList().Select(x => x.ColumnName).ToList(); // Column Names List List RowList = dt.Rows.Cast().ToList(); foreach (DataRow dr in RowList) { writer.WriteStartElement(RowName); for (int i = 0; i x.ColumnName).ToList(); // Column Names List List RowList = dt.Rows.Cast().ToList(); foreach (DataRow dr in RowList) { writer.WriteStartElement(RowName); for (int i = 0; i in và click sự kiện Convert file XMLusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace ConvertXmlApp{ public partial class ConvertXmlFrom : Form { public ConvertXmlFrom() { InitializeComponent(); } private void btnBrowseFolder_Click(object sender, EventArgs e) { DialogResult drResult = OFD.ShowDialog(); if (drResult == System.Windows.Forms.DialogResult.OK) txtXlFilePath.Text = OFD.FileName; } private void btnConvert_Click(object sender, EventArgs e) { if (chkCustomeName.Checked && txtCustomeFileName.Text != “” && txtXlFilePath.Text!=”” && txtNodeName.Text!=””) // using Custome Xml File Name { if (File.Exists(txtXlFilePath.Text)) { string CustXmlFilePath = Path.Combine(new FileInfo(txtXlFilePath.Text).DirectoryName, txtCustomeFileName.Text); // Ceating Path for Xml File ConvertXml _Convert = new ConvertXml(); _Convert.CreateXltoXML(CustXmlFilePath, txtXlFilePath.Text, txtNodeName.Text); // Xml File, EXcel File, Row Name MessageBox.Show(“Conversion Completed!!”); } } else if (txtXlFilePath.Text != “” && txtNodeName.Text != “”) // Using Default Xml File Name { if (File.Exists(txtXlFilePath.Text)) { ConvertXml _Convert = new ConvertXml(); _Convert.CreateXltoXML(txtXlFilePath.Text, txtNodeName.Text); MessageBox.Show(“Conversion Completed!!”); } } else { MessageBox.Show(“Please Fill Required Feilds!!”); } } }}Chi tiết các bạn có thể hướng dẫn source để tham khảo nhé.

Xem thêm bài viết thuộc chuyên mục: Excel