protected void btnExport_Click(object sender, EventArgs e){string path1 = HttpContext.Current.Server.MapPath(\". \") + \"\\\\3_StuInfo.txt\";string path = Server.MapPath(\"~/2_District.txt\");StreamWriter sw = new StreamWriter(path);try{sw.WriteLine(string.Format(\"{0}的{1}下辖{2}个县区:\", ddlProvince.SelectedItem.Text, ddlCity.SelectedItem.Text, ddlDistrict.Items.Count));foreach (ListItem item in ddlDistrict.Items){sw.WriteLine(string.Format(\"{0}(ID={1})\", item.Text, item.Value));}}catch (Exception error){labTips.Text = \"导出失败请重试\";}finally {sw.Close();labTips.Text = \"导出数据成功!\";}}
要点
创建数据流
StreamWriter sw = new StreamWriter(path);
写入数据
sw.WriteLine();
关闭流
sw.Close();