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)
{
{
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});
}
}
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);
xlWorkBook.Close(false);
xlApp.Quit();
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)
{
}