if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string str = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(str);
SqlCommand sqlcom = new SqlCommand();
sqlcom.Connection = sqlcon;
sqlcon.Open();
sqlcom.CommandText = "Select * from T_Customer";
SqlDataAdapter sda = new SqlDataAdapter(sqlcom);
DataSet ds = new DataSet();
sda.Fill(ds);
int RC = ds.Tables[0].Rows.Count;//得到数据的行数
string[] lz = new string[RC];
for (int j = 0; j < RC; j++)//j是所得数据表的行数
{
DataRow dr = ds.Tables[0].Rows[j];
for (int i = 1; i < dr.ItemArray.Length; i++)//i是列数
{
//判断读取的是不是空值,是空值则转换为“还未填写”
if (dr.ItemArray[i].ToString() == "")
{
//判断读取是不是最后一列,是最后一列后面就不添加“|”
if (i==dr.ItemArray.Length-1)
{
lz[j] += "未填写";
}
else
{
lz[j] += "未填写";
lz[j] += "|";
}
}
else
{
if (i == dr.ItemArray.Length - 1)
{
lz[j] += dr.ItemArray[i].ToString();
}
else
{
lz[j] += dr.ItemArray[i].ToString();
lz[j] += "|";
}
}
}
}
string filename = saveFileDialog1.FileName;
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));//通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
sw.Flush();
sw.BaseStream.Seek(0, SeekOrigin.Begin);
string ts = "姓名" + "|" + "电话1" + "|" + "电话2" + "|" + "E-mail" + "|" + "手机1" + "|" + "手机2" + "|" + "地址" + "|" + "邮编" + "|" + "车牌号" + "|" + "车架号" + "|" + "车型" + "|" + "门店" + "|" + "购车时间" + "|" + "建议" + "|" + "备注";
sw.WriteLine(ts);
for (int i = 0; i < RC; i++)
{
sw.WriteLine(lz[i]);
}
sw.Flush();
sw.Close();
sqlcon.Close();
MessageBox.Show("导出完成!");
}
{
string str = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(str);
SqlCommand sqlcom = new SqlCommand();
sqlcom.Connection = sqlcon;
sqlcon.Open();
sqlcom.CommandText = "Select * from T_Customer";
SqlDataAdapter sda = new SqlDataAdapter(sqlcom);
DataSet ds = new DataSet();
sda.Fill(ds);
int RC = ds.Tables[0].Rows.Count;//得到数据的行数
string[] lz = new string[RC];
for (int j = 0; j < RC; j++)//j是所得数据表的行数
{
DataRow dr = ds.Tables[0].Rows[j];
for (int i = 1; i < dr.ItemArray.Length; i++)//i是列数
{
//判断读取的是不是空值,是空值则转换为“还未填写”
if (dr.ItemArray[i].ToString() == "")
{
//判断读取是不是最后一列,是最后一列后面就不添加“|”
if (i==dr.ItemArray.Length-1)
{
lz[j] += "未填写";
}
else
{
lz[j] += "未填写";
lz[j] += "|";
}
}
else
{
if (i == dr.ItemArray.Length - 1)
{
lz[j] += dr.ItemArray[i].ToString();
}
else
{
lz[j] += dr.ItemArray[i].ToString();
lz[j] += "|";
}
}
}
}
string filename = saveFileDialog1.FileName;
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));//通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
sw.Flush();
sw.BaseStream.Seek(0, SeekOrigin.Begin);
string ts = "姓名" + "|" + "电话1" + "|" + "电话2" + "|" + "E-mail" + "|" + "手机1" + "|" + "手机2" + "|" + "地址" + "|" + "邮编" + "|" + "车牌号" + "|" + "车架号" + "|" + "车型" + "|" + "门店" + "|" + "购车时间" + "|" + "建议" + "|" + "备注";
sw.WriteLine(ts);
for (int i = 0; i < RC; i++)
{
sw.WriteLine(lz[i]);
}
sw.Flush();
sw.Close();
sqlcon.Close();
MessageBox.Show("导出完成!");
}