2个回答
展开全部
#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;
}
/// <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哦?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
{
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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询