Hi Bindu,
Thanks for providing us the template file.
There is slight error in your scenario. You are importing datatable columns to the worksheet but there is a column which is merged (D-G........as you described), and after the column there is another column H. So, your data would be divided into five columns if I am right i.e.., "A", "B", "C", "D-G" and "H". When the data is imported using the code i.e.., worksheet.Cells.ImportDataTable(dt, false, "A27"); some new (blank )rows would be inserted and data would be imported to those rows, so, apparently A-E columns would be filled and F-H columns would be continuously empty. And also, you are getting "the cells out of range are also getting merged and there is slight distortion" I think, that's why, below the data table, you are getting those merged range of cells, it not it?
I think you can perform your task in the following ways.
1). Fill the data cell by cell using your own code from datatable after merging the cells. Please make sure that you are not filling the data into the merged/hidden cells (E-G cells) in the column
2). You may try to use Cells.ImportDataColumn method to import the columns one by one into the desired cells to implement your scenario. I use the following sample code which works fine, you may try to consult it. The input and output files are attached here.
Sample code:
Workbook workbook = new Workbook();
workbook.Open("f:\\test\\mergeproblem.xls");
Worksheet sheet = workbook.Worksheets[0];
Cells cells = sheet.Cells;
DataTable dataTable1 = new DataTable();
dataTable1.Columns.Add("Cus_IP",typeof(string));
dataTable1.Columns.Add("FUND#",typeof(string));
dataTable1.Columns.Add("Material_Proxy",typeof(string));
dataTable1.Columns.Add("Fund_Name",typeof(string));
dataTable1.Columns.Add("Quantity",typeof(string));
for (int i = 0; i <= 40; i++)
{
DataRow dr = dataTable1.NewRow();
for (int j = 0; j < 5; j++)
{
dr[j] = "AsposeTest" + i.ToString() + j.ToString();
}
dataTable1.Rows.Add(dr);
}
int s2 = 0;
string cell2 = null;
string cell1 = null;
Aspose.Cells.Range range;
for (int i = 0; i <= (dataTable1.Rows.Count - 1); i++)
{
s2 = 28 + i;
cell1 = "D" + s2.ToString();
cell2 = "G" +s2.ToString();
range = cells.CreateRange(cell1, cell2);
range.Merge();
}
int index = 0;
for (int i = 0; i < dataTable1.Columns.Count; i++)
{
//Since D-G columns are merged so, we have to mange that the last column should be filled
//on H column.
if (i > 3)
{
sheet.Cells.ImportDataColumn(dataTable1, false, 27, 7, i, false);
}
sheet.Cells.ImportDataColumn(dataTable1, false, 27, index, i, false);
index = index + 1;
}
sheet.AutoFitColumns();
workbook.Save("f:\\test\\mergecellsrangetest.xls");
3). You may try to use Aspose.Cells' Smart Marker feature, set/place smart markers into different cells as column headers and process the markers to fill the data into the cells, check for your reference: http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/smart-markers.html
Thank you.