/// <summary>
/// 分解数据表
/// </summary>
/// <param name="originalTab">需要分解的表</param>
/// <param name="rowsNum">每个表包含的数据量</param>
/// <returns></returns>
public static List<DataTable> SplitDataTable(DataTable originalTab, int rowsNum)
{
//获取所需创建的表数量
int tableNum = originalTab.Rows.Count / rowsNum;
//获取数据余数
int remainder = originalTab.Rows.Count % rowsNum;
List<DataTable> ds = new List<DataTable>();
//如果只需要创建1个表,直接将原始表存入DataSet
if (tableNum == 0)
{
ds.Add(originalTab);
}
else
{
DataTable[] tableSlice = new DataTable[tableNum];
for (int c = 0; c < tableNum; c++)
{
tableSlice[c] = new DataTable();
foreach (DataColumn dc in originalTab.Columns)
{
tableSlice[c].Columns.Add(dc.ColumnName, dc.DataType);
}
}
for (int i = 0; i < tableNum; i++)
{
if (i != tableNum - 1)
{ for (int j = i * rowsNum; j < ((i + 1) * rowsNum); j++)
{
tableSlice[i].ImportRow(originalTab.Rows[j]);
}
}
else
{
for (int k = i * rowsNum; k < ((i + 1) * rowsNum + remainder); k++)
{
tableSlice[i].ImportRow(originalTab.Rows[k]);
}
}
}
foreach (DataTable dt in tableSlice)
{
ds.Add(dt);
}
}
return ds;
}

  

05-11 21:56