请问,如何DataGrid控件内容如何导入到Excel中?(里边有LinkButton控件)?
如下:
姓名 年龄 住址 修改
aa 11 aa linkbutton
bb 22 bb linkbutton
请问如何将查询到的DataGrid内容导入到Excel中。
看过以前类似的文章,当到入时显示错误为:
“DataGridLinkButton”的控件“dg__ctl3__ctl1”必须放在具有 runat=server 的窗体标记内。
也不知道说明白没,请大家帮忙,感谢参与!
如下:
姓名 年龄 住址 修改
aa 11 aa linkbutton
bb 22 bb linkbutton
请问如何将查询到的DataGrid内容导入到Excel中。
看过以前类似的文章,当到入时显示错误为:
“DataGridLinkButton”的控件“dg__ctl3__ctl1”必须放在具有 runat=server 的窗体标记内。
也不知道说明白没,请大家帮忙,感谢参与!
如果datagrid中有linkbutton列,导出程序就会报错
类型“DataGridLinkButton”的控件“DataGrid1__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。纠正这个错误的方法就是对datagrid进行重新绑定,当然在重新绑定之前最好设置参数,这样查询的时候是LinkButton列,导出的时候是普通列或不生成该列.
我实现的方法,修改导出函数如下:
Session["print"]="1";
SetBind();//重新绑定datagrid
Session.Remove("print");//注意:清理session的部分不能写在DataGrid1_ItemDataBound中
//因为DataGrid1_ItemDataBound是一行一行绑的.如果写在里面就
//表示绑定了第一行,等第二行的时候还是会绑定LinkButton.Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.DataGrid1.Page.EnableViewState =false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End(); private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(Session["print"] !=null)
{
if(Session["print"].ToString()=="1")
{
e.Item.Cells[0].Visible =false;
e.Item.Cells[1].Visible =false;
e.Item.Cells[2].Visible =false;
e.Item.Cells[12].Visible =false;
}
}
}
SetBind();//重新绑定datagrid(这个函数是怎么写的?)
Session.Remove("print");//
这个函数DataGrid1_ItemDataBound()功能是否是将“把linkButton隐藏起来”?
其他的我都改了。可以导出了。但导出的excel文件,显示“不能读取文件”。确定后,里边没内容。
/// 导出数据到Excel
/// </summary>
/// <param name="dTable">要导出的数据集</param>
/// <param name="OutPutTitle">导出数据标题</param>
/// <param name="OutDirecotrPath">导出文件路径(../UpLoadFiles/BB/PDF/)</param>
/// <param name="OutPutFileName">导出文件名</param>
/// <param name="OutPutHeadName">数据表头</param>
/// <param name="FilePath">导出文件路径</param>
/// <returns></returns>
public bool OutPutExcel(ref DataTable dTable, string OutDirecotrPath, string OutPutTitle, string OutPutFileName, string OutPutHeadName, out string FilePath)
{
try
{
FilePath = ""; //****引用外部类,进行输入数据设置
UFIDAFramework.CommonLibrary.Reports.ExcelAccess.ExcelAccess mExcelAccess = new UFIDAFramework.CommonLibrary.Reports.ExcelAccess.ExcelAccess();
//****打开Excel
mExcelAccess.Open(); //****设置字体大小
System.Drawing.Font font = new System.Drawing.Font("黑体", 21, FontStyle.Bold);
System.Drawing.Font pFont = new System.Drawing.Font("宋体", 12, FontStyle.Regular); //****获取Table行数
int intColumnCount = dTable.Columns.Count;
//****获取记录总数
int intRowCount = dTable.Rows.Count;
string ReptTitle = ""; //****设置报表名称
if (OutPutTitle == "")
{
ReptTitle = "包机收入数据";
}
else
{
ReptTitle = OutPutTitle;
} //****子舱位收入报表表头,同时写入报表报表数据
mExcelAccess.MergeCells(1, 1, 1, intColumnCount);
mExcelAccess.SetFont(1, 1, 1, intColumnCount, font);
mExcelAccess.SetCellText(1, 1, 1, intColumnCount, ReptTitle);
mExcelAccess.SetColumnWidth(1, 12f); //****设置表头
string ReptHeadName = OutPutHeadName; int indexHeaderRow = 0; string[] DBParamates = ReptHeadName.Split(',');
foreach (string mHeaderName in DBParamates)
{
indexHeaderRow++;
//****加载表头处理
mExcelAccess.SetCellText(2, indexHeaderRow, mHeaderName);
} //****第三行开如输出数据
int intDataStartRowIndex = 3; for (int i = 0; i < dTable.Rows.Count; i++)
{
for (int j = 0; j < dTable.Columns.Count; j++)
{
//****设置单元格值
if (dTable.Rows[i][j].GetType().ToString() == "System.DateTime")
{
//****转抽象日期样式
mExcelAccess.SetCellText(intDataStartRowIndex + i, j + 1, Convert.ToDateTime(dTable.Rows[i][j].ToString()).ToShortDateString());
}
else
{
//****显示数据 mExcelAccess.SetCellText(intDataStartRowIndex + i, j + 1, dTable.Rows[i][j].ToString());
}
}
} //****画边框及网格线
mExcelAccess.SetBordersEdge(1, 1, dTable.Rows.Count + 2, dTable.Columns.Count, false); //****构造文件名
string FileName = UFIDAFramework.CommonLibrary.DevelopLibrary.Develops.Strings.BulidAutoDateCode(4); //****保存文件到导出文件临时目录中
string mDirecotrPath = OutDirecotrPath; //****获取文件夹路径
string DirecotrPath = Server.MapPath(mDirecotrPath); if (Directory.Exists(DirecotrPath) == false)
{
//****创建文件夹
Directory.CreateDirectory(DirecotrPath);
} string mSaveFiletPath = ""; if (OutPutFileName == "")
{
mSaveFiletPath = DirecotrPath + "\\" + FileName + ".xls";
}
else
{
mSaveFiletPath = DirecotrPath + "\\" + OutPutFileName + ".xls"; //****验证文件是否存在,如果存在,则删除
if (File.Exists(mSaveFiletPath) == true)
{
//****删除文件处理
File.Delete(mSaveFiletPath);
} } //****保存文件
mExcelAccess.SaveAs(mSaveFiletPath, false); //****显示Excel
//mExcelAccess.ShowExcel(); font.Dispose(); //***关闭报表
mExcelAccess.Close(); FilePath= mSaveFiletPath; return true;
}
catch
{
FilePath = ""; return false;
} }