我正在c#中创建一个xlsx到xml转换器窗体应用程序我正在获取输入" .xlsx"文件并导出" .xml"文件使用" XmlTextWriter"我希望合并我的两列Excel工作表,命名为"日期"和"时间"在"日期和时间"的一栏中;我怎么能这样做?请帮忙,因为我是c#的新手。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel;
using System.Xml;
using System.IO;
namespace XLSXtoXMLConvertor
{
public partial class XLSXtoXMLConvertor : Form
{
public XLSXtoXMLConvertor()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Load_Excel_File(textBox1.Text);
}
public void Load_Excel_File(string file)
{
if (string.IsNullOrEmpty(file) || !File.Exists(file))
{
MessageBox.Show("The Excel file is invalid / No FIle Selected! Please select a valid file.");
return;
}
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(file, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
//Map_Table = new DataTable();
for (int j = 1; j <= range.Columns.Count; j++) // Header Names
{
xlWorkSheet.Range["H1"].Value2 = "Fueling Station";
xlWorkSheet.Range["C1"].Value2 = "Comments";
xlWorkSheet.Range["D1"].Value2 = "Current Odometer Reading";
xlWorkSheet.Range["M1"].Value2 = "Vehicle Number";
xlWorkSheet.Range["N1"].Value2 = "Quantity";
xlWorkSheet.Range["O1"].Value2 = "Cost per Gallon";
xlWorkSheet.Range["P1"].Value2 = "Total Cost";
xlWorkSheet.Range["F1"].Value2 = "Date";
xlWorkSheet.Range["G1"].Value2 = "Time";
// Use if want to get column headers.
}
//DataRow dataRow = null;
XmlTextWriter txtWriter = null;
string test = "C:\\Users\\muhammad.irfan\\Desktop\\" + "Mileage and Fuel Usage.xml";//save xml file
txtWriter = new XmlTextWriter(test, null);
//string FileName = saveFileDialog1.FileName.ToString();
//XmlTextWriter txtWriter = new XmlTextWriter(saveFileDialog1.FileName, Encoding.UTF8);
//txtWriter.WriteStartDocument(true);
//StringWriter s = new StringWriter();
//txtWriter = XmlWriter.Create(s);
//try
//{
txtWriter.Formatting = Formatting.Indented;
txtWriter.Indentation = 6;
txtWriter.Namespaces = false;
txtWriter.WriteStartDocument();
txtWriter.WriteStartElement("", "Root", ""); // start Tag
txtWriter.WriteStartElement("", "Tag", ""); // Inside
// for (int row = 1; row < range.Rows.Count + 1; row++)
for (int row = 1; row < range.Rows.Count; row++)
{
// for (int col = 1; col <= range.Columns.Count; col++)
for (int col = 1; col <= range.Columns.Count; col++)
{
if ((col.Equals(1) || (col.Equals(2) || (col.Equals(5) || (col.Equals(9) || (col.Equals(10) || (col.Equals(11) || (col.Equals(12) || (col.Equals(17) || (col.Equals(18)))))))))))
{
continue;
}
string test1 = (string)(range.Cells[row, col] as Excel.Range).Text.ToString();
if (test1 != null)
// richTextBox1((range.Cells[row, col] as Excel.Range).Text.ToString());
//richTextBox1.AppendText((range.Cells[row, col] as Excel.Range).Text.ToString());
txtWriter.WriteStartElement(((range.Cells[col] as Excel.Range).Text.ToString()));
txtWriter.WriteString(((range.Cells[row, col] as Excel.Range).Text.ToString()));
txtWriter.WriteEndElement();
}
}
txtWriter.WriteEndElement(); // end Tag
txtWriter.WriteEndElement(); // end Inside
txtWriter.Flush();
txtWriter.Close();
MessageBox.Show("Converted Successfully!");
try
{
xlWorkBook.Close(false, null, null);
xlApp.Quit();
}
catch (Exception ex)
{
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
}
finally
{
GC.Collect();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
// Initializes a OpenFileDialog instance
using (OpenFileDialog openfileDialog = new OpenFileDialog())
{
openfileDialog.RestoreDirectory = true;
openfileDialog.Filter = "Excel files(*.xlsx;*.xls)|*.xlsx;*.xls";
if (openfileDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openfileDialog.FileName;
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//using (SaveFileDialog savefiledialog = new SaveFileDialog())
//{
// savefiledialog.RestoreDirectory = true;
// savefiledialog.DefaultExt = "xml";
// savefiledialog.Filter = "All Files(*.xml)|*.xml";
// if (savefiledialog.ShowDialog() == DialogResult.OK)
// {
// /*
// Stream filestream = savefiledialog.OpenFile();
// StreamWriter streamwriter = new StreamWriter(filestream);
// streamwriter.Write("<?xml version='1.0'?>" +
// Environment.NewLine + textBox1.Text.ToString());
// streamwriter.Close();*/
// }
//}
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
}
}