1.首先是如何将数据从 xls文件加载到winform的DataGridView控件中。
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog fileDLG = new OpenFileDialog();
- fileDLG.Title = "Open Excel File";
- fileDLG.Filter = "Excel Files|*.xls;*.xlsx";
- fileDLG.InitialDirectory = @"C:\Users\...\Desktop\";
- if (fileDLG.ShowDialog() == DialogResult.OK)
- {
- string filename = System.IO.Path.GetFileName(fileDLG.FileName);
- string path = System.IO.Path.GetDirectoryName(fileDLG.FileName);
- excelLocationTB.Text = @path + "\\" + filename;
- string ExcelFile = @excelLocationTB.Text;
- if (!File.Exists(ExcelFile))
- MessageBox.Show(String.Format("File {0} does not Exist", ExcelFile));
- OleDbConnection theConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFile + ";Extended Properties=Excel 12.0;");
- theConnection.Open();
- OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", theConnection);
- DataSet DS = new DataSet();
- theDataAdapter.Fill(DS, "ExcelInfo");
- dataGridView1.DataSource = DS.Tables["ExcelInfo"];
- formatDataGrid();
- MessageBox.Show("Excel File Loaded");
- toolStripProgressBar1.Value += 0;
- }
- }
- private void formatDataGrid()
- {
- dataGridView1.ColumnHeadersVisible = true;
- dataGridView1.Columns[0].Name = "Path Name";
- dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
- }
- if (dataGridView1.Rows.Count == 0)
- {
- MessageBox.Show("No data available!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- else
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "CSV files (*.csv)|*.csv";
- saveFileDialog.FilterIndex = 0;
- saveFileDialog.RestoreDirectory = true;
- saveFileDialog.CreatePrompt = true;
- saveFileDialog.FileName = null;
- saveFileDialog.Title = "Save path of the file to be exported";
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- Stream myStream = saveFileDialog.OpenFile();
- StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
- string strLine = "";
- try
- {
- //Write in the headers of the columns.
- for (int i = 0; i < dataGridView1.ColumnCount; i++)
- {
- if (i > 0)
- strLine += ",";
- strLine += dataGridView1.Columns[i].HeaderText;
- }
- strLine.Remove(strLine.Length - 1);
- sw.WriteLine(strLine);
- strLine = "";
- //Write in the content of the columns.
- for (int j = 0; j < dataGridView1.Rows.Count; j++)
- {
- strLine = "";
- for (int k = 0; k < dataGridView1.Columns.Count; k++)
- {
- if (k > 0)
- strLine += ",";
- if (dataGridView1.Rows[j].Cells[k].Value == null)
- strLine += "";
- else
- {
- string m = dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
- strLine += m.Replace(",", ",");
- }
- }
- strLine.Remove(strLine.Length - 1);
- sw.WriteLine(strLine);
- //Update the Progess Bar.
- toolStripProgressBar1.Value = 100 * (j + 1) / dataGridView1.Rows.Count;
- }
- sw.Close();
- myStream.Close();
- MessageBox.Show("Data has been exported to:" + saveFileDialog.FileName.ToString(), "Exporting Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
- toolStripProgressBar1.Value = 0;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Exporting Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- }
- Output to XML file format using StreamWriting Object.
- if (dataGridView1.Rows.Count == 0)
- {
- MessageBox.Show("No data available!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- else
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "XML files (*.xml)|*.xml";
- saveFileDialog.FilterIndex = 0;
- saveFileDialog.RestoreDirectory = true;
- saveFileDialog.CreatePrompt = true;
- saveFileDialog.FileName = null;
- saveFileDialog.Title = "Save path of the file to be exported";
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- Stream myStream = saveFileDialog.OpenFile();
- StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
- try
- {
- sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
- sw.WriteLine("<NewXML>");
- for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
- {
- sw.WriteLine("<Row" + i + ">");
- for (int j = 0; j < dataGridView1.Columns.Count; j++)
- sw.WriteLine("<" + dataGridView1.Columns[j].HeaderText + ">" + dataGridView1.Rows[i].Cells[j].Value.ToString().Trim() + "</" + dataGridView1.Columns[j].HeaderText + ">");
- sw.WriteLine("</Row" + i + ">");
- //Update the Progess Bar.
- toolStripProgressBar1.Value = 100 * (i + 1) / (dataGridView1.Rows.Count - 1);
- }
- sw.WriteLine("</NewXML>");
- sw.Close();
- myStream.Close();
- MessageBox.Show("Data has been exported to:" + saveFileDialog.FileName.ToString(), "Exporting Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
- toolStripProgressBar1.Value = 0;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Exporting Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- }