感谢您的查看和帮助!
我有多张excel表格,我想将这多张excel,根据相同的表头合并到一张excel里面。
测试文件和结果:测试.zip (31.6 KB)
MS Excel 或 Aspose.Cells 中没有用于任务的内置选项或自动方式。 您必须设计自己的代码和逻辑来完成任务。 我认为您可以通过 Range.Copy() 方法将数据范围从源工作簿工作表相应地复制到目标工作簿。 请参阅以下示例代码供您参考。
例如
示例代码:
string filePath = "g:\\test2\\";
Workbook destWorkbook = new Workbook();
Worksheet destSheet = destWorkbook.Worksheets[0];
int TotalRowCount = 0;
//first workbook
Workbook workbook = new Workbook(filePath + "1.xlsx");
Worksheet sourceSheet = workbook.Worksheets[0];
Aspose.Cells.Range sourceRange = sourceSheet.Cells.MaxDisplayRange;
Aspose.Cells.Range destRange = destSheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn, sourceRange.RowCount, sourceRange.ColumnCount);
destRange.Copy(sourceRange);
TotalRowCount = sourceRange.RowCount + TotalRowCount;
//second workbook
workbook = new Workbook(filePath + "2.xls");
sourceSheet = workbook.Worksheets[0];
sourceRange = sourceSheet.Cells.MaxDisplayRange;
destRange = destSheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn, sourceRange.RowCount, sourceRange.ColumnCount);
destRange.Copy(sourceRange);
destRange.Worksheet.Cells.DeleteRow(sourceRange.FirstRow + TotalRowCount);//remove header row
TotalRowCount = sourceRange.RowCount + TotalRowCount;
//Your code goes here.
destWorkbook.Save(filePath + "out1.xlsx");
请参考上面的代码,根据您的自定义需求编写/更新您自己的代码。
希望这对您有帮助。
@fhn123456,
你可以通过范围拷贝来实现需求。通过分析你的文件数据,列之间并没有完全对应。你需要根据第一行的数据来判断拷贝范围。拷贝完成的数据,第一列还需要根据需求做自动定增。关于范围拷贝,你可以参考以下文档:
关于你的目标需求,请参考以下样例代码:
private static void MergeFiles()
{
string filePath = "";
Workbook wb1 = new Workbook(filePath + "1.xlsx");
Workbook wb2 = new Workbook(filePath + "2.xls");
Workbook wb3 = new Workbook(filePath + "3.xlsx");
Cells cells1 = wb1.Worksheets[0].Cells;
Cells cells2 = wb2.Worksheets[0].Cells;
Cells cells3 = wb3.Worksheets[0].Cells;
Hashtable table = new Hashtable();
Row firstRow1 = cells1.Rows[0];
IEnumerator ie1 = firstRow1.GetEnumerator();
while (ie1.MoveNext())
{
Cell currCell = (Cell)ie1.Current;
table.Add(currCell.StringValue, currCell.Column);
}
//需要根据行号改变的列名
string sequenceName = "序号";
CopyRangeByColumn(cells1, cells2, table, sequenceName);
CopyRangeByColumn(cells1, cells3, table, sequenceName);
wb1.Save(filePath + "out_net.xlsx");
}
private static void CopyRangeByColumn(Cells cells1, Cells cells2, Hashtable table, string sequenceName)
{
Range maxRange1 = cells1.MaxDisplayRange;
int copyStartRow = maxRange1.FirstRow + maxRange1.RowCount;
Range maxRange2 = cells2.MaxDisplayRange;
Row firstRow2 = cells2.Rows[0];
IEnumerator ie2 = firstRow2.GetEnumerator();
while (ie2.MoveNext())
{
Cell currCell = (Cell)ie2.Current;
int column = (int)table[currCell.StringValue];
Range tempRange = cells2.CreateRange(maxRange2.FirstRow + 1, currCell.Column, maxRange2.RowCount - 1, 1);
Range copyRange = cells1.CreateRange(copyStartRow, column, tempRange.RowCount, 1);
copyRange.Copy(tempRange);
//根据行号填充序列号
if (sequenceName == currCell.StringValue)
{
int startInex = copyRange.FirstRow;
// 获取范围迭代器
IEnumerator rangeEnumerator = copyRange.GetEnumerator();
// 获取单元格
while (rangeEnumerator.MoveNext())
{
Cell cell = rangeEnumerator.Current as Aspose.Cells.Cell;
cell.Value = startInex++;
}
}
}
}
希望这些能够帮助到你。
@fnaeem,
通过使用Aspose.Cells for .NET 23.9运行前面提供的代码,我们能得到正确的结果文件。请查看附件 (55.8 KB)。
如果你仍然遇到问题,请分享你的信息,包括但不限于操作系统,.NET版本等。