调用方法
DataTable dt = new DataTable();//调试用
dt = 将一个二维数组转换城datatable(arrayList);//调试用
public DataTable 将一个二维数组转换城datatable(string[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)//arr.GetLength(0) 表示行
{
for (int j = 0; j < arr.GetLength(1); j++)//arr.GetLength(1) 表示列
{
if (arr[i, j] == null)
{
arr[i, j] = "";
}
}
}
DataTable dataSouce = new DataTable();
for (int i = 0; i < arr.GetLength(1); i++)
{
DataColumn newColumn = new DataColumn(i.ToString(), arr[0, 0].GetType());
dataSouce.Columns.Add(newColumn);
}
for (int i = 0; i < arr.GetLength(0); i++)
{
DataRow newRow = dataSouce.NewRow();
for (int j = 0; j < arr.GetLength(1); j++)
{
newRow[j.ToString()] = arr[i, j];
}
dataSouce.Rows.Add(newRow);
}
return dataSouce;
对于数组中有null 转为“”这种模式