将gridpanel数据导入到Excel中

  将coolite中gridpanel中的数据导入到本地excel中时,默认表格的表头不会随数据一起下载,下面就教你如何将表头随数据一起下载到本地excel中.

  (1)store hidden gridpanel三种控件的配合使用 hidden的ID="hidAlarmHistory" gridpanel的ID="grdAlarmHistory",在head标签中加入以下js代码:

  var saveAlarmData = function() {
hidAlarmHistory.setValue(Ext.encode(grdAlarmHistory.getRowsValues(false)));
}

  (2)在gridpanel中的每列<column>中必须加入:

<PrepareCommand Handler="" Args="grid,command,record,row,col,value" FormatHandler="False">
</PrepareCommand>

  (3)选择模式必须加入<SelectionModel><ext:RowSelectionModel></ext:RowSelectionModel></SelectionModel>;

  (4)按钮属性如下:AutoPostBack必须置为true;

<ext:Button ID="Button3" runat="server" Text="To Excel" OnClick="btnToExcel1" AutoPostBack="true" Icon="PageExcel">
<Listeners>
<Click Handler="saveAlarmData();" />
</Listeners>
</ext:Button>

  (5)代码如下:

  

View Code
 public void ToExcel(GridPanel gp, string fileName, DataTable dt, HttpResponse resp)
{
resp.Clear();
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
resp.ContentType = "application nd.ms-excel";

string colHeaders = "", ls_item = "";
int i = 0;
int cl = gp.ColumnModel.Columns.Count - 1;
string columns = "";

//输出表头
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
if (!gp.ColumnModel.Columns[i].Hidden)
{
colHeaders += gp.ColumnModel.Columns[i].Header;
columns += gp.ColumnModel.Columns[i].DataIndex + ",";
}
colHeaders += "\n";
}
else
{
if (!gp.ColumnModel.Columns[i].Hidden && gp.ColumnModel.Columns[i].ColumnID != "update")
{
colHeaders += gp.ColumnModel.Columns[i].Header + "\t";
columns += gp.ColumnModel.Columns[i].DataIndex + ",";
}
}
}
resp.Write(colHeaders);

if (columns != "") columns = columns.Substring(0, columns.LastIndexOf(","));
string[] columnall = columns.Split(',');

cl = columnall.Length ;

//逐行处理数据
for (int j = 0; j < dt.Rows.Count; j++)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))
{
ls_item += EditString(dt.Rows[j][columnall[i].ToString()].ToString()) + "\n";

}
else
{
ls_item += EditString(dt.Rows[j][columnall[i].ToString()].ToString()) + "\t";
}
}
resp.Write(ls_item);
ls_item = "";
}
Response.Flush();
resp.End();
}
private static string EditString(string text1)//处理字符问题
{


if (text1.IndexOf('\r') != -1)
{
text1 = text1.Replace('\r', '');
}
if (text1.IndexOf('\n') != -1)
{
text1 = text1.Replace('\n', '');
}
if (text1.IndexOf('\t') != -1)
{
text1 = text1.Replace('\t', '');
}
if (text1.IndexOf("'") != -1)
{
text1 = text1.Replace("'", "");
}
if (text1.IndexOf("T") != -1)
{
text1 = text1.Replace("T", "");
}

if (text1.IndexOf(@"""") != -1)
{
text1 = text1.Replace(@"""", "");
}
return text1;


}
protected void btnToExcel1(object sender, EventArgs e)
{
string json = hidAlarmHistory.Value.ToString();
bool addHeader = true;
DataTable dt = new DataTable();
StringBuilder sbstr = new StringBuilder();
Dictionary<string, string>[] companies = JSON.Deserialize<Dictionary<string, string>[]>(json);
foreach (Dictionary<string, string> row in companies)
{

if (addHeader)
{
foreach (KeyValuePair<string, string> keyValuePair in row)
{
dt.Columns.Add(keyValuePair.Key);
}
}
addHeader = false;
ArrayList list = new ArrayList();
DataRow dr = dt.NewRow();
foreach (KeyValuePair<string, string> keyValuePair in row)
{

dr[keyValuePair.Key] = keyValuePair.Value;
if (dr["ah_id"] == dr[keyValuePair.Key])
{
sbstr.Append(keyValuePair.Value + ";");
}
}
dt.Rows.Add(dr);
}
ToExcel(grdAlarmHistory,"date",dt, this.Response);
}

 

posted @ 2011-11-07 09:07  Samguist  阅读(1147)  评论(0编辑  收藏  举报