system: win 10
I setup a excel workbook, and merge a lots cells,
then I use CellRange[] rangeList = sheet.MergedCells; to get RangeList,
and then I use foreach (CellRange srcRange in rangeList) srcRange.UnMerge(); to unMerge the cell
at last, I want to copy the content to fill each cell that used to be merged.
neither I use sheet.Copy(rangeFirst, rangeItem, true);
or I get the RangeAddressLocal and then use sheet.Copy(sheet.Range[fromLocal], sheet.Range[itemAddress], true);
It does not work correct.
when I physicly use sheet.Copy(sheet.Range["A3"], sheet.Range["A4"], true);
It works correct.
I don't know how to attach the sample file or images.
here is the code:
- Code: Select all
string file = @"D:\Excel\test.xlsx";
Workbook workbook = new Workbook();
workbook.LoadFromFile(file);
Worksheet sheet = workbook.Worksheets[0];
CellRange[] rangeList = sheet.MergedCells;
foreach (CellRange srcRange in rangeList)
{
string fromLocal = srcRange.Cells.First().RangeAddressLocal;
//string toLocal = srcRange.Cells.Last().RangeAddressLocal;
srcRange.UnMerge();
CellRange rangeFirst = srcRange.Cells.First();
foreach (CellRange rangeItem in srcRange)
{
string itemAddress = rangeItem.RangeAddressLocal;
//if (fromLocal != itemAddress)
// sheet.Copy(sheet.Range[fromLocal], sheet.Range[itemAddress], true);
if (rangeItem != rangeFirst)
sheet.Copy(rangeFirst, rangeItem, true);
}
}
workbook.SaveToFile(file, ExcelVersion.Version2010);
MessageBox.Show("Done!");