Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, i have an app where i import excel file then convert it to XML file
Now the importing process works really fine but i need to create a button to do this process below

Export button Click >> Shows safe dialog >> Select Path >> Type file name >> Save as XML

What I have tried:

 private void IMPORT_Click(object sender, EventArgs e)
       {
           {
               //Fill datagridview  from excel file

               Microsoft.Office.Interop.Excel.Application xlApp;
               Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
               Microsoft.Office.Interop.Excel.Worksheet xlWorlSheet;
               Microsoft.Office.Interop.Excel.Range xlRange;
               int xlRow;
               string strfileName;
               openFileDialog1.Filter = "Excel Office | *.xls; *.xlsx";
               openFileDialog1.ShowDialog();
               strfileName = openFileDialog1.FileName;
               if (strfileName != string.Empty)
               {
                   xlApp = new Microsoft.Office.Interop.Excel.Application();
                   xlWorkBook = xlApp.Workbooks.Open(strfileName);
                   xlWorlSheet = xlWorkBook.Worksheets["sheet1"];
                   xlRange = xlWorlSheet.UsedRange;
                   const string FILENAME = @"c:\temp\xml file.xml";
                   System.Data.DataTable dt = new System.Data.DataTable();
                   dt.Columns.Add("CLM1", typeof(string));
                   dt.Columns.Add("CLM2", typeof(string));
                   dt.Columns.Add("CLM3", typeof(string));
                   dt.Columns.Add("CLM4", typeof(string));
                   dt.Columns.Add("CLM5", typeof(string));
                   dt.Columns.Add("CLM6", typeof(string));
                   dt.Columns.Add("CLM7", typeof(string));
                   dt.Columns.Add("CLM8", typeof(string));
                   dt.Columns.Add("CLM9", typeof(string));
                   dt.Columns.Add("CLM10", typeof(string));
                   dt.Columns.Add("CLM11", typeof(string));
                   dt.Columns.Add("CLM12", typeof(DateTime));
                   dt.Columns.Add("CLM13", typeof(DateTime));
                       for (xlRow = 2; xlRow <= xlRange.Rows.Count; xlRow++)
                   {
                           if (xlRange.Cells[xlRow, 2].Text != "")
                       {
                           dt.Rows.Add(new object[] {
                       xlRange.Cells[xlRow, 1].Text,xlRange.Cells[xlRow, 2].Text, xlRange.Cells[xlRow, 3].Text, xlRange.Cells[xlRow, 4].Text,
                       xlRange.Cells[xlRow, 5].Text, xlRange.Cells[xlRow, 6].Text,xlRange.Cells[xlRow, 7].Text, xlRange.Cells[xlRow, 8].Text,
                       xlRange.Cells[xlRow, 9].Text, xlRange.Cells[xlRow, 10].Text,xlRange.Cells[xlRow, 11].Text, xlRange.Cells[xlRow, 12].Text,
                       xlRange.Cells[xlRow, 13].Text});
                       }
                   }

                   // XML PARSING USING XDocument

                   dataGridView1.DataSource = dt;
                   var d = Convert.ToDecimal("1.2345", new CultureInfo("en-US"));
                   XDocument doc = new XDocument(
                   new XDeclaration("1.0", "ISO-8859-1", null),
                   new XElement("Root",
                   new XElement("textBox", textBox.Text),
                   new XElement("comboBox1", combo1.Text),
                   new XElement("comboBox2", combo2.Text),
                   new XElement("comboBox3", combo3.ValueMember),
                   new XElement("Table",
                   dt.AsEnumerable().Select(row => new XElement("rd", new object[]{
                   new XElement("idd",row[0]),
                   new XElement("num",row[1]),
                   new XElement("des",row[2]),
                   new XElement("mht",row[3]),
                   new XElement("vat",row[4]),
                   new XElement("ctt",row[5]),
                   new XElement("refF",new object[] {
                   new XElement("if",row[6]),
                   new XElement("onm",row[7]),
                   new XElement("iccc",row[8]),}),
                   new XElement("tx",row[9]),
                   new XElement("mp",new XElement("id", row[10])),
                   new XElement("dpai",row.Field<DateTime>("CLM12").ToString("yyyy-MM-dd")),
                   new XElement("dcac",row.Field<DateTime>("CLM13").ToString("yyyy-MM-dd"))
                   })))));

                   doc.Save(FILENAME);

                   // Cleanup
                   xlWorkBook.Close(false);
                   xlApp.Quit();
                   // Manual disposal because of COM
                   while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) != 0) { }
                   while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) != 0) { }
                   while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorlSheet) != 0) { }
                   while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange) != 0) { }

                   xlApp = null;
                   xlWorkBook = null;
                   xlWorlSheet = null;
                   xlRange = null;

                   GC.Collect();
                   GC.WaitForPendingFinalizers();

                   dataGridView1.DataSource = dt;
               }
           }
       }


private void EXPORT_Click(object sender, EventArgs e)
       {

       }
Posted
Updated 17-May-20 15:42pm

1 solution

probably

1) Get a file/path using a dialog

SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments); 
saveFileDialog1.Filter = "Your extension here (*.XML)|*.xml|All Files (*.*)|*.*" ; 
saveFileDialog1.FilterIndex = 1; 

if(saveFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    // Use saveFileDialog1.FileName);
}


2) It's not clear what 'document' you want saved, but, assuming 'document' was of type XDocument, then
document.Save(saveFileDialog1.FileName); 

in the
Quote:
// Use saveFileDialog1.FileName);
from above is probably what you're looking for, ie

3) The entire code assuming you're set document somewhere in your code ...

SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments); 
saveFileDialog1.Filter = "Your extension here (*.XML)|*.xml|All Files (*.*)|*.*" ; 
saveFileDialog1.FilterIndex = 1; 

if(saveFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    document.Save(saveFileDialog1.FileName);
}
 
Share this answer
 
Comments
Usarsef 17-May-20 23:17pm    
the document i try to save is this one created on xdocument >> XDocument doc = new XDocument...

now the error is : The name 'doc' does not exist in the current context
Garth J Lancaster 18-May-20 0:02am    
It seems like a scoping issue - if you've declared your 'doc' instance local to another handler or such .. you really need to declare it somewhere 'global', so it can be 'loaded' ? Imported in your terms in one handler, then Exported by the handler you're working on ... there's usually a spot somewhere within a WinForms app you can declare such, but Im on a Mac at the moment without Visual Studio, so cant check....
Usarsef 18-May-20 0:16am    
I solved the problem thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900