永发信息网

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

答案:3  悬赏:70  手机版
解决时间 2021-02-13 09:33
  • 提问者网友:轻浮
  • 2021-02-13 02:22
txt如何导出?望回复代码。谢谢。
最佳答案
  • 五星知识达人网友:玩家
  • 2021-02-13 03:52
#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;
}
全部回答
  • 1楼网友:詩光轨車
  • 2021-02-13 05:50
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(); }
  • 2楼网友:一秋
  • 2021-02-13 05:17
protected void button1_click(object sender, eventargs e) { //设定导出文件的格式 response.contenttype = "application/vnd.ms-excel"; //设定编码方式 response.charset = "gb2312"; response.contentencoding = system.text.encoding.utf7; //关闭viewstate enableviewstate = false; stringwriter tw = new stringwriter(); htmltextwriter hw = new htmltextwriter(tw); gridview1.rendercontrol(hw); //把html写回浏览器 response.write(tw.tostring()); response.end(); } 注意要在命名空间中引用using system.io; 在你这个事件后添加如下代码: public override void verifyrenderinginserverform(control control) { } 以上代码已经修改过,将其复制到你的vs里,绝对正确,这可是经过测试过的哟!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯