c#.net中把gridview中当前的数据导出成xls或者txt保存到客户端

txt如何导出?望回复代码。谢谢。... txt如何导出?望回复代码。谢谢。 展开
 我来答
ty68351114
2011-09-01 · TA获得超过716个赞
知道答主
回答量:253
采纳率:0%
帮助的人:150万
展开全部
#region 将DataGridView控件中数据导出到Excel
/// <summary>
/// 将DataGridView控件中数据导出到Excel
/// </summary>
/// <param name="gridView">DataGridView对象</param>
/// <param name="isShowExcle">是否显示Excel界面</param>
/// <returns></returns>
public static bool ExportDataGridview(DataGridView gridView, bool isShowExcle, string path)
{
bool result = false;
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook myWorkBook = null;
try
{
if (gridView.Rows.Count == 0)
return false;
//建立Excel对象
excel = new Microsoft.Office.Interop.Excel.Application();
myWorkBook = excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;

//生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
//填充数据
for (int i = 0; i < gridView.Rows.Count - 1; i++)
{
for (int j = 0; j < gridView.Columns.Count; j++)
{
if (gridView[j, i].ValueType == typeof(string))
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
else
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
object missing = System.Reflection.Missing.Value;
excel.ActiveWorkbook.Saved = true;
excel.ActiveWorkbook.SaveAs(path,
Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8,
missing, missing, false,
false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
myWorkBook.Close(missing, missing, missing);
myWorkBook = null;
excel.Quit();
result = true;
}
catch (System.Exception ex)
{
excel.Quit();
XException xe = FuncLib.BuildXExcpetion("将DataGridView控件中数据导出到Excel出错", ex);
xe.AddEnvironmentData("path", path, true);
xe.WriteLogRecord(false, true, DateTime.Now.ToString("yyyy-MM-dd"));
}
return result;
}
追问
这还是导出excel,我说的是TXT哦?
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Glad大明白
2011-09-01 · TA获得超过1223个赞
知道小有建树答主
回答量:750
采纳率:75%
帮助的人:186万
展开全部
public void Totxt(System.Web.UI.Control ctl, string FileName)
{
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//HttpContext.Current.Response.ContentType = "application/ms-excel";application/ms-txt
HttpContext.Current.Response.ContentType = "application/ms-txt";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".txt");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式