添加新的 sheet 到 Excel 工作簿以在行限制 > 1M 时存储数据
Add new sheet to Excel workbook to store data when row limit > 1M
我正在尝试使用 Spire.Xls 将一些数据存储到 excel 文件中。
将多个文件中的数据复制到另一个 excel 文件的第一个 sheet 时,我想 创建一个新的 excel sheet 当到达 行 = 1,048,575 时,将数据粘贴到这个新的 sheet 中。这是我的代码:
Workbook tempbook = new Workbook();
tempbook.LoadFromFile(PathToSecondFile);
Workbook workbook = new Workbook();
workbook.LoadFromFile(PathToFirstFile);
//import the second workbook's worksheet into the first workbook using a datatable
Worksheet sheet2 = tempbook.Worksheets[0];
//copy data from sheet2 into a datatable
DataTable dataTable = sheet2.ExportDataTable();
//load sheet1
Worksheet sheet1 = workbook.Worksheets[0];
var c1 = sheet1.LastRow;
var c2 = sheet2.LastRow;
if (c1 >= 1048575 || c2 >= 1048575 || (c1 + c2) >= 1048575)
{
//create a new worksheet and append data into it but
//at this line getting Index out of bound exception
Worksheet sheet3 = workbook.Worksheets.Add("NewSheet");
sheet3.InsertDataTable(dataTable, false, sheet3.LastRow + 1, 1);
}
else
{
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}
}
即使代码超越异常,数据也保存到sheet2,但是到sheet1的数据不保存。欢迎所有建议。提前致谢。
是否可以切换到 Excel 2007+ 格式?通过这种方式,您可以找到很多 excel 的提供程序,例如 Apache poi、Spreadsheet Light。
实际上这很简单...有点解决方法就完成了任务。所以这是代码:
int c1 = workbook.ActiveSheet.LastRow, c2 = tempbook.Worksheets[0].LastRow;
if ((c1 + c2) <= 1048575)
{
//import the second workbook's worksheet into the first workbook using a datatable
//load 1st sheet of tempbook into sheet
Worksheet sheet = tempbook.Worksheets[0];
//copy data from sheet into a datatable
DataTable dataTable = sheet.ExportDataTable();
//load sheet1
Worksheet sheet1 = workbook.Worksheets[workbook.ActiveSheetIndex];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}
else if ((c1 >= 1048575 && c2 >= 1048575) || c1 >= 1048575 || c2 >= 1048575 || (c1 + c2) >= 1048575)
{
workbook.Worksheets.AddCopy(tempbook.Worksheets[0]);
indx = workbook.ActiveSheet.Index;
workbook.ActiveSheetIndex = ++indx;
}
else
{
//import the second workbook's worksheet into the first workbook using a datatable
//load 1st sheet of tempbook into sheet
Worksheet sheet = tempbook.Worksheets[0];
//copy data from sheet into a datatable
DataTable dataTable = sheet.ExportDataTable();
//load sheet1
Worksheet sheet1 = workbook.Worksheets[workbook.ActiveSheetIndex];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}
显然,if 和 else 块代码是相同的。但也想不出别的了。欢迎修改。
我正在尝试使用 Spire.Xls 将一些数据存储到 excel 文件中。
将多个文件中的数据复制到另一个 excel 文件的第一个 sheet 时,我想 创建一个新的 excel sheet 当到达 行 = 1,048,575 时,将数据粘贴到这个新的 sheet 中。这是我的代码:
Workbook tempbook = new Workbook();
tempbook.LoadFromFile(PathToSecondFile);
Workbook workbook = new Workbook();
workbook.LoadFromFile(PathToFirstFile);
//import the second workbook's worksheet into the first workbook using a datatable
Worksheet sheet2 = tempbook.Worksheets[0];
//copy data from sheet2 into a datatable
DataTable dataTable = sheet2.ExportDataTable();
//load sheet1
Worksheet sheet1 = workbook.Worksheets[0];
var c1 = sheet1.LastRow;
var c2 = sheet2.LastRow;
if (c1 >= 1048575 || c2 >= 1048575 || (c1 + c2) >= 1048575)
{
//create a new worksheet and append data into it but
//at this line getting Index out of bound exception
Worksheet sheet3 = workbook.Worksheets.Add("NewSheet");
sheet3.InsertDataTable(dataTable, false, sheet3.LastRow + 1, 1);
}
else
{
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}
}
即使代码超越异常,数据也保存到sheet2,但是到sheet1的数据不保存。欢迎所有建议。提前致谢。
是否可以切换到 Excel 2007+ 格式?通过这种方式,您可以找到很多 excel 的提供程序,例如 Apache poi、Spreadsheet Light。
实际上这很简单...有点解决方法就完成了任务。所以这是代码:
int c1 = workbook.ActiveSheet.LastRow, c2 = tempbook.Worksheets[0].LastRow;
if ((c1 + c2) <= 1048575)
{
//import the second workbook's worksheet into the first workbook using a datatable
//load 1st sheet of tempbook into sheet
Worksheet sheet = tempbook.Worksheets[0];
//copy data from sheet into a datatable
DataTable dataTable = sheet.ExportDataTable();
//load sheet1
Worksheet sheet1 = workbook.Worksheets[workbook.ActiveSheetIndex];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}
else if ((c1 >= 1048575 && c2 >= 1048575) || c1 >= 1048575 || c2 >= 1048575 || (c1 + c2) >= 1048575)
{
workbook.Worksheets.AddCopy(tempbook.Worksheets[0]);
indx = workbook.ActiveSheet.Index;
workbook.ActiveSheetIndex = ++indx;
}
else
{
//import the second workbook's worksheet into the first workbook using a datatable
//load 1st sheet of tempbook into sheet
Worksheet sheet = tempbook.Worksheets[0];
//copy data from sheet into a datatable
DataTable dataTable = sheet.ExportDataTable();
//load sheet1
Worksheet sheet1 = workbook.Worksheets[workbook.ActiveSheetIndex];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}
显然,if 和 else 块代码是相同的。但也想不出别的了。欢迎修改。