刚刚上传了一个txt文件转换Excel文件的一个工具http://download.csdn.net/detail/zydsk0126/6973603;所以我要紧接着写一个Excel转换txt文件的小Demo,方便大家运用.
我写的这个小Demo需要引用到using System.Data.OleDb和using System.IO;
在这里,就用我做的项目中一个Excel表来做底料啊,这个表中有字段"部门代码,部门名称,商品条码,商品代码,名称,单位,规格,库存数量,最后进价,零售单价"。
新建一个winfrom程序
string fileName = "";
private void OpenExcel_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
fd.Title = "选择Excel文件";
fd.Filter = "xls files(*.xls)|*.xls";
if (fd.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = fd.FileName;
}
}
private void ExcelToText_Click(object sender, EventArgs e)
{
string mystring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.textBox1.Text.ToString() +";Extended Properties=Excel 8.0;";
OleDbConnection oledbconn = null;
DataTable dt = null;
string BMDM = "";
string BMMC = "";
string SPDM = "";
string SPTM = "";
string MC = "";
string DW = "";
string GG = "";
string KCSL = "";
string ZHJJ = "";
string DJ = "";
try
{
//初始化连接,并打开
oledbconn = new OleDbConnection(mystring);
oledbconn.Open();
dt = oledbconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
//获取表名
string name = dt.Rows[0]["table_name"].ToString().Trim();
//sql语句
string strsql = "select * from [" + name + "]";
OleDbCommand oledbcomm = new OleDbCommand(strsql, oledbconn); //执行命令
OleDbDataAdapter oledbda = new OleDbDataAdapter(oledbcomm); //适配器
DataSet ds = new DataSet(); //数据集
oledbda.Fill(ds); //将数据填充到dataset中
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i][0].ToString() == "部门代码")
continue;
else
{
BMDM = ds.Tables[0].Rows[i]["部门代码"].ToString();
BMMC = ds.Tables[0].Rows[i]["部门名称"].ToString();
SPDM = ds.Tables[0].Rows[i]["商品代码"].ToString();
SPTM = ds.Tables[0].Rows[i]["商品条码"].ToString();
MC = ds.Tables[0].Rows[i]["名称"].ToString();
DW = ds.Tables[0].Rows[i]["单位"].ToString();
GG = ds.Tables[0].Rows[i]["规格"].ToString();
KCSL = ds.Tables[0].Rows[i]["库存数量"].ToString();
ZHJJ = ds.Tables[0].Rows[i]["最后进价"].ToString();
DJ = ds.Tables[0].Rows[i]["零售单价"].ToString();
WriteText(BMDM.ToString(), BMMC.ToString(), SPDM.ToString(), SPTM.ToString(), MC.ToString(), DW.ToString(), GG.ToString(), KCSL.ToString(), ZHJJ.ToString(), DJ.ToString());
}
}
}
MessageBox.Show("转换成功!");
}
catch (Exception ex)
{
}
}
private void WriteText(string BMDM, string BMMC, string SPDM, string SPTM, string MC, string DW, string GG,string KCSL,string ZHJJ,string DJ)
{
//if (Directory.Exists(fileName) == true)
//{
// Directory.Delete(fileName);
//}
//else
//{
//}
StreamWriter Sw = new StreamWriter(fileName, true, Encoding.Default);
Sw.Write(SPTM.ToString() + ",");
Sw.Write(BMDM.ToString() + ",");
Sw.Write(BMMC.ToString() + ",");
Sw.Write(SPDM.ToString() + ",");
Sw.Write(MC.ToString() + ",");
Sw.Write(DW.ToString() + ",");
Sw.Write(GG.ToString() + ",");
Sw.Write(KCSL.ToString() + ",");
Sw.Write(ZHJJ.ToString() + ",");
Sw.Write(DJ.ToString());
Sw.WriteLine();
Sw.Flush();
Sw.Close();
Sw.Dispose();
}
//选择生成路径
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog f = new FolderBrowserDialog();
if (f.ShowDialog() == DialogResult.OK)
{
string path = f.SelectedPath;
this.textBox2.Text = path;
fileName = path + "pd.txt";
}
}