Want to extract a table from excel using apsose cell and write it in aspose word

Hello Team,
I am using aspose word and cell I want to extract the content between 2 cells completely and paste the same way however it is in my aspose word (It can be a table) so I want to handle this kind of scenario including merge cells hidden cells everything should be handled is it possible

@Raghul214,

I think you can export your desired range (of cells) in the worksheet as HTML (using Aspose.Cells) and then insert/import that exported HTML into MS Word document via Aspose.Words API. Another approach can be save ranges to PDF via Aspose.Cells and then output PDF pages to Word document via Aspose.Words or Aspose.PDF.

By the way, Aspose.Cells now supports Excel to DOCX feature, so you could try to use it if you want to export the whole range directly but as a new document (all by Aspose.Cells).

See the sample code on how to render a range to HTML via Aspose.Cells for your reference.

using (var fstream = new FileStream("e:\\test2\\TestExport.xlsx", FileMode.Open))
{
	// Instantiate a Workbook object that represents the existing Excel file
	var workbook = new Workbook(fstream);
	
	var sheet = workbook.Worksheets["Sheet1"];

	workbook.Worksheets.ActiveSheetIndex = sheet.Index;

    var range = sheet.Cells.CreateRange(1, 0, 10, 10); 

	sheet.PageSetup.PrintArea = range.Address;

	// Save the Excel file to HTML
	workbook.Save("e:\\test2\\sample.html", new HtmlSaveOptions
	{
		ExportImagesAsBase64 = true,
		ExportPrintAreaOnly = true,
		ExportHiddenWorksheet = false,
		ExportActiveWorksheetOnly = true
	});
}

Hope, this helps a bit.

Yes, that would be a great idea thanks for that :blush:

@Keerthana_K_R ,

You are welcome. If you have any further queries or comments, please feel free to write back to us.

1 Like

Hello @amjad.sahi Good afternoon, this works completely fine but in case i have collapsed some rows its not working is there a way where in i can make that also available?

@Keerthana_K_R,

Yes, if the rows are hidden or collapsed, they won’t be shown in the output HTML. So, you need to unhide or expand those rows before rendering to HTML.

Yes manually if I do that will work and its not possible through code is my understanding correct?

@Keerthana_K_R,

Unhiding rows/cols is possible via Aspose.Cells APIs. see the document for your reference. If you are still unable to achieve this in your code or have a different scenario/case, please share your template Excel file (please zip the file before attaching it here) and we will investigate it promptly.

Ok sure let me go through the document in case if i find some difficulties will let you know thanks for the same😊

@Keerthana_K_R,

Sure, please take your time to evaluate and implement your task. Please feel free to write back to us if you have any queries or issues.

1 Like

Hello @amjad.sahi Good Afternoon!!!
If I wanted to extract range of cells means from A1 to C3 and G1 to J3 and join their contents how can I achieve that. Like is there a way I can achieve that

I want to join because I want the table as the same way by which we are doing in html format so that my style every thing is preserved

@Raghul214,

See the following sample code for your reference.
e.g.,
Sample code:

using (var fstream = new FileStream("e:\\test2\\TestExport.xlsx", FileMode.Open))
{
	// Instantiate a Workbook object that represents the existing Excel file
	var workbook = new Workbook(fstream);
	
	var sheet = workbook.Worksheets["Sheet1"];

	workbook.Worksheets.ActiveSheetIndex = sheet.Index;

    var range1 = sheet.Cells.CreateRange(0, 0, 3, 3); 
    var range2 = sheet.Cells.CreateRange("G1:J3");

    
    sheet.PageSetup.PrintArea = range1.Address + "," + range2.Address;

	// Save the Excel file to HTML
	workbook.Save("e:\\test2\\out1.html", new HtmlSaveOptions
	{
		ExportImagesAsBase64 = true,
		ExportPrintAreaOnly = true,
		ExportHiddenWorksheet = false,
		ExportActiveWorksheetOnly = true
	});
} 

Yes Hello @amjad.sahi I tried the above but it was not in a continuous way like I can see its way apart
I just want it together

@Raghul214,

Could you please share your input Excel file, the output HTML (generated by Aspose.Cells), and a sample (runnable) code to evaluate your issue? Also, please share the expected output HTML file that you want to achieve. We will look into it soon.

PS. Please zip the files before attaching them here.

Input
image.png (174.9 KB)
Output
image.png (111.8 KB)
But I want like
image.png (169.5 KB)
Since I am not able to upload excel file I added screenshot

@Raghul214,

Thanks for the screenshots.

I do not think this is possible automatically when you specify multiple print areas, unless you paste your other range manually with the first range of cells. You can try your scenario in MS Excel manually and you will notice that the other range will be separated and won’t be merged in the print preview unless you merge it manually using copy/paste operations. In short, if you need to merge ranges, you may need to cut or copy/paste the ranges. See the document with example code for your reference. Once you have merged/inserted the other range with your first range, then you may set the print area covering all the data of both ranges. Now render to HTML using the code segment and it will work for the purpose.

Hmm ok I will try that and thank you for responding early😊

@Raghul214,

You are welcome.

1 Like

Hello @amjad.sahi I am able to achieve this but one is like if i have content in a cell which is not shown completely only the thing which is visible is getting shown in html so i did autofitrow and column its working but still some part is Missing how to retrieve the entire content of all cells in a range

And also here if I want to copy and paste only cells which is not hidden how to achieve that