//导出全部
protected void btnExport_Click(object sender, EventArgs e)
{
DataView dv = DataSource.DefaultView;
Dictionary<string, string> cols = new Dictionary<string, string>();
foreach (JQGridColumn col in JQGrid1.Columns)
{
if (col.Visible)
{
cols.Add(col.DataField, col.HeaderText);
}
}
if (DataSource != null)
{
StringWriter sw = HelpClass.Export(DataSource, cols);
Response.AddHeader("Content-Disposition", "attachment; filename="+DateTime.Now.ToString("yyyyMMddHHssmm")+".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
}
}
public class HelpClass
{
public static StringWriter Export(DataTable dt, Dictionary<string, string> cols)
{
StringWriter sw = new StringWriter();
int i = 0;
StringBuilder HeaderName = new StringBuilder();
foreach (string Name in cols.Values)
{
HeaderName.Append(Name);
if (i != cols.Count - 1)
{
HeaderName.Append("\t");
}
}
sw.WriteLine(HeaderName);
foreach (DataRow dr in dt.Rows)
{
StringBuilder RowsData = new StringBuilder();
int k = 0;
foreach (string value in cols.Keys)
{
RowsData.Append(dr[value]);
if (k != cols.Count - 1)
{
RowsData.Append("\t");
}
}
sw.WriteLine(RowsData);
}
sw.Close();
return sw;
}
}