
int percent = ( int )((( double )progressBar1.Value / ( double )progressBar1.Maximum) * 100). if (progressBar1.Value < progressBar1.Maximum). Worksheet excelWorkSheet = (.Worksheet)book.ActiveSheet. .Application excel = new .Application(). private void ExportDataTableToExcel( table, string Xlfile). This function returns a DataTable and we export this datatable into the Excel file. In ‘CreateDataTableFromXml’ function, we are simply converting the XML data into ‘DataTable’.
MessageBox.Show( "Please Fill Required Feilds!!" ). string XlFile = fi.DirectoryName + "\\" + fi.Name.Replace(fi.Extension,". FileInfo fi = new FileInfo(txtXmlFilePath.Text). else if (!chkCustomeName.Checked || txtXmlFilePath.Text != "" ) // Using Default Xml File Name. MessageBox.Show( "Conversion Completed!!" ). ExportDataTableToExcel(dt, CustXmlFilePath). dt = CreateDataTableFromXml(txtXmlFilePath.Text). string CustXmlFilePath = Path.Combine( new FileInfo(txtXmlFilePath.Text).DirectoryName, txtCustomeFileName.Text) // Ceating Path for Xml Files. if (chkCustomeName.Checked & txtCustomeFileName.Text != "" & txtXmlFilePath.Text != "" ) // using Custome Xml File Name. private void btnConvert_Click(object sender, EventArgs e). Now, code the click event of the button to browse the computer files so that the user can select the desired XML file.
So, here, our UI looks like the following. And, another button is for converting the XML file to Excel. And the ProgressBar is used to show the progress of the conversion. A button is used to browse the XML file and checkbox to give an option to the user for custom Excel file name. Now, make a UI which contains two Buttons, two Textboxes, one CheckBox, a single ProgressBar, and an OpenFileDialog tool. You will find this reference in ‘COM’ Section. The targeted audience is people with basic knowledge of C#.Ĭreate a new project and give it a suitable name as I gave the project name - ‘ConvertXmlToXl’.Īfter creating the project, you have to add a reference named “Microsoft Excel Object library” in your project. We are going to make a small single-form application which takes an XML file from the user and converts that XML Data to Excel Data and returns an Excel file. Here, we are going to learn to read XML files in C# Windows.Form applications. In this article, I am going to share with you the method of converting an XML file to Excel in C#.