Tout, j'ai un utilitaire pour exporter DataTables vers Excel (en tous les deux .xls et les types de fichiers .xlsx). Lorsque j'atteins une limite de colonne ou de ligne sur le DataTable
(extrait de SQL Server), je veux diviser le DataTable
en enfants, chacun respectant les limites (pour les .xls 256 colonnes et 65 536 lignes et .xlsx 16 384 colonnes et 1 048 576 lignes) .Fractionner un DataTable dans plusieurs DataTables
Jusqu'à présent, j'ai écrit la méthode suivante pour faire ce que je veux
public static List<DataTable> SplitDataTable(DataTable mother, int nColLimit)
{
List<int[]> rangeList = new List<int[]>();
int primaryCols = mother.Columns.Count;
int nSplitCount = Convert.ToInt32(primaryCols/nColLimit);
int max = -1;
// Get the child ranges.
int tmpSup = 0;
for (int splits = 0; splits < nSplitCount; splits++)
{
if (rangeList.Count == 0)
tmpSup = (splits + 1) * (nColLimit - 1);
else
tmpSup = rangeList[splits - 1][1] + nColLimit;
rangeList.Add(new int[2] { splits * nColLimit, tmpSup });
if (max < tmpSup)
max = tmpSup;
}
rangeList.Add(new int[2] { ++max, primaryCols });
// Build child DataTables.
List<DataTable> childList = new List<DataTable>();
int childIndex = 0;
foreach (int[] range in rangeList)
{
childList.Add(new DataTable());
for (int i = range[0]; i < range[1]; i++)
for (int j = 0; j < mother.Rows.Count; j++)
childList[childIndex].Rows[j][i] = mother.Rows[j][i];
childIndex++;
}
return childList;
}
Ceci, cependant, un indexOutOfRangeException
jette avec le message « Il n'y a pas de ligne à la position 0. ». J'apprécie d'où vient l'erreur, mais quelle est la meilleure façon de copier les colonnes complètes du mother
à un enfant?
J'ai aussi essayé
List<DataTable> childList = new List<DataTable>();
int childIndex = 0;
foreach (int[] range in rangeList)
{
childList.Add(new DataTable());
foreach(DataRow row in mother.Rows)
{
DataRow tmpRow = childList[childIndex].NewRow();
for (int i = range[0]; i < range[1]; i++)
tmpRow[i + 1] = row[i + 1];
}
childIndex++;
}
qui donnent la même exception de gamme.
Merci pour votre temps.
Edit: comment je l'ai fait à court terme
foreach (int[] range in rangeList)
{
childList.Add(new DataTable());
string strSqlTmp =
String.Format("declare @columns varchar(max) " +
"select @columns = case when @columns is null " +
"then '' " +
"else @columns + ', ' " +
"end + name " +
"from sys.columns " +
"where object_id = object_id('{0}') and name in " +
"(SELECT COLUMN_NAME " +
"FROM [{1}].INFORMATION_SCHEMA.COLUMNS " +
"WHERE TABLE_NAME = N'{0}' " +
"AND ORDINAL_POSITION > {2} AND ORDINAL_POSITION < {3}) " +
"declare @query varchar(max) " +
"set @query = 'select ' + @columns + ' from {0}' " +
"exec (@query);
// Then get each DataTable from SQL Server and fill the child list...
Merci beaucoup pour votre temps. Pendant ce temps, j'ai skimpé et demandé à SQL Server de le faire. J'ai édité ma question pour montrer comment ceci a été fait si vous avez intéressé ... – MoonKnight
heureux que vous l'ayez fonctionné et merci pour signaler votre solution. –