October 6, 2016

Excel to xml Conversion

public partial class frmXmlConversion : Form
    {
        private string strFileName;
        private clsExcelToXML obj;
        private string strSheetNameRange;
        private DataSet dsFile;
        string filepath;
        string Newxmlfilename;
        string fileNameWithoutex;

        public frmXmlConversion()
        {
            InitializeComponent();
        }

        private void frmXmlConversion_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.Activate();
            Application.DoEvents();
        }

        private void frmXmlConversion_Activated(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = FormBorderStyle.None;
        }

        private void btnBro_Click(object sender, EventArgs e)
        {
            try
            {
                dlgOpenFile.Filter = "Excel File(*.xls)|*.xls";
                dlgOpenFile.ShowDialog();
                strFileName = dlgOpenFile.FileName;
                obj = new clsExcelToXML(strFileName, false);
                txtFileName.Text = strFileName;
                string[] sheetnames = obj.GetExcelSheetNames();
                cboSheetName.Items.Clear();
                cboSheetName.Items.AddRange(sheetnames);
                cboSheetName.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            CreateXmlForBarcode();
            CreateXmlForCase();
        }

        private void CreateXmlForCase()
        {
            try
            {
                dsFile = new DataSet();
                DataTable dt = new DataTable();
                strSheetNameRange = cboSheetName.Text.ToString();

                dt = obj.GetDataTable(strSheetNameRange);

                if (dt.Rows.Count > 0)
                {
                    XmlWriterSettings contentTypesSettings = new XmlWriterSettings();
                    contentTypesSettings.Indent = true;

                    filepath = txtFileName.Text;
                    fileNameWithoutex = Path.Combine(Path.GetDirectoryName(filepath), Path.GetFileNameWithoutExtension(filepath));
                    Newxmlfilename = fileNameWithoutex + "_Case.xml";
                    XmlWriter xmlWriter = XmlWriter.Create(Newxmlfilename);

                    xmlWriter.WriteStartDocument(true);
                    xmlWriter.WriteStartElement("UNMAPPED_DATASET");

                    string chk = string.Empty;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (chk != dr["F1"].ToString())
                        {
                            if (chk != string.Empty)
                                chk = dr["F1"].ToString();

                            xmlWriter.WriteStartElement("CASE");
                            xmlWriter.WriteAttributeString("GTIN", dr["F1"].ToString());
                            xmlWriter.WriteAttributeString("ETN", dr["F5"].ToString());
                            xmlWriter.WriteEndElement();
                        }
                    }
                    if (xmlWriter != null)
                        xmlWriter.Flush();

                    xmlWriter.Close();

                }
                Functions.setMessageBox(Functions.gApp, "Both files exported Sucessfully", 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void CreateXmlForBarcode()
        {
            try
            {
                dsFile = new DataSet();
                DataTable dt = new DataTable();
                strSheetNameRange = cboSheetName.Text.ToString();

                dt = obj.GetDataTable(strSheetNameRange);

                if (dt.Rows.Count > 0)
                {
                    XmlWriterSettings contentTypesSettings = new XmlWriterSettings();
                    contentTypesSettings.Indent = true;

                    filepath = txtFileName.Text;
                    fileNameWithoutex = Path.Combine(Path.GetDirectoryName(filepath), Path.GetFileNameWithoutExtension(filepath));
                    Newxmlfilename = fileNameWithoutex + "_Barcode.xml";
                    XmlWriter xmlWriter = XmlWriter.Create(Newxmlfilename);

                    xmlWriter.WriteStartDocument(true);
                    xmlWriter.WriteStartElement("UNMAPPED_DATASET");
                   
                    string chk = string.Empty;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (chk != dr["F6"].ToString())
                        {
                            if (chk != string.Empty)
                            chk = dr["F6"].ToString();
                          
                            xmlWriter.WriteStartElement("BOTTLE");
                            xmlWriter.WriteAttributeString("BARCODE", dr["F6"].ToString());
                            xmlWriter.WriteEndElement();
                        }
                    }
                    if (xmlWriter != null)
                        xmlWriter.Flush();

                    xmlWriter.Close();

                }
              
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

No comments:

Post a Comment