復制代碼 代碼如下:
/// <summary>
/// DataTable行轉列
/// </summary>
/// <param name="dtable">需要轉換的表</param>
/// <param name="head">轉換表表頭對應舊表字段(小寫)</param>
/// <returns></returns>
public static DataTable DataTableRowtoCon(DataTable dtable, string head)
{
DataTable dt = new DataTable();
dt.Columns.Add("NumberID");
for (int i = 0; i < dtable.Rows.Count; i++)
{//設置表頭
dt.Columns.Add(dtable.Rows[i][head].ToString());
}
for (int k = 0; k < dtable.Columns.Count; k++)
{
string temcol = dtable.Columns[k].ToString();
if (dtable.Columns[k].ToString().ToLower() != head)//過濾掉設置表頭的列
{
DataRow new_dr = dt.NewRow();
new_dr[0] = dtable.Columns[k].ToString();
for (int j = 0; j < dtable.Rows.Count; j++)
{
string temp = dtable.Rows[j][k].ToString();
new_dr[j + 1] = (Object)temp;
}
dt.Rows.Add(new_dr);
}
}
return dt;
}