乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > c#.net中把gridview中当前的数据导出成xls或者...-.net导出数据到excel,net导出excel

c#.net中把gridview中当前的数据导出成xls或者...-.net导出数据到excel,net导出excel

作者:乔山办公网日期:

返回目录:excel表格制作




#region 将DataGridView控件中数据导出到e799bee5baa6e59b9ee7ad94335Excel
/// <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;
}
按条件查询(支持组合查询)在gridview里已经能显示出来,需要导入excel和页面打印的功能,请大虾们帮帮忙~~~

/// <summary>
/// 导出Datatable的数据到excel
/// </summary>
/// <param name="dt">datatable</param>
private void CreateExcel(DataTable dt)
{
try
{
string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + ".xls";
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
resp.ContentType = "application/ms-excel";
string colHeaders = "", ls_item = "";

//定义表对象与行对象,同时用DataSet对其值进行初始化
DataRow[] myRow = dt.Select();
int i = 0;
int cl = dt.Columns.Count;

//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加636f7079e799bee5baa6338回车符
colHeaders += dt.Columns[1].Caption.Replace("ID", "编号") + "\t";
colHeaders += dt.Columns[2].Caption.Replace("FuncID", "操作名称") + "\t";
colHeaders += dt.Columns[0].Caption.Replace("logtypes", "操作类型") + "\t";
colHeaders += dt.Columns[6].Caption.Replace("LogDec", "操作内容") + "\t";
colHeaders += dt.Columns[5].Caption.Replace("UserID", "用户名称") + "\t";
colHeaders += dt.Columns[4].Caption.Replace("CreateTime", "操作时间") + "\n";
resp.Write(colHeaders);

//向HTTP输出流中写入取得的数据信息
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < dt.Rows.Count; i++)
{
//if (i == (cl - 1))//最后一列,加n
ls_item += dt.Rows[i][1].ToString() + "\t";
ls_item += dt.Rows[i][2].ToString() + "\t";
ls_item += dt.Rows[i][0].ToString() + "\t";
ls_item += dt.Rows[i][6].ToString().Replace("\r\n", "") + "\t";
if (dt.Rows[i][5].ToString().Trim() == "")
{
ls_item += "无姓名" + "\t";
}
else
{
ls_item += dt.Rows[i][5].ToString() + "\t";
}
ls_item += dt.Rows[i][4].ToString() + "\n";

}
resp.Write(ls_item);
ls_item = "";
resp.End();
}
catch (Exception)
{

throw;
}

}
protected void btiiii_Click(object sender, EventArgs e)
{
try
{
DCExecl(Convert.ToInt32(this.txtstrPageindex.Value), Convert.ToInt32(this.txtendPageindex.Value));
new Facade.SystemLog().SaveSystemLogCode("系统日志",(int)Facade.EnumMuster.LOG_TYPE.DEVICE_OPERATION,Session["UserID"].ToString(),Session["UserID"].ToString()+"导出系统日志数据");
}
catch (Exception)
{

throw;
}
}
}

适当修改就行·

相关阅读

  • Asp.Net <em>MVC</em> <em>导出excel</em>

  • 乔山办公网excel表格制作
  • mvc导出excel时不弹出框下载,而是在后台..." src="/uploads/tu/216.jpg" style="width: 400px; height: 267px;" />建议使用微软报表,使用RDLC,可以在MVC中生成报表,然后支持导出e799bee5baa6e997aee7ad9
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网