We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert Range in a WS to CSV String in Java

Hi,

I have a Range like:
Range range = workbook.getWorksheets().get(“list”).getCells().createRange(2, 0, 10, 3);

I want to convert the range contents to a CSV String.

Is it possible?

@sandipanghosh111,

There are a few approaches to accomplish the task for your needs.

  1. Copy that range to some other sheet and then make it as an active sheet, or even in some other new workbook and save to CSV file format. You might already know that in CSV file format, only single (active) sheet will be exported/saved. See the sample code segment for your reference:
    e.g
    Sample code:

    Workbook workbook = new Workbook(“f:\files\CopySample.xlsx”);
    Worksheet worksheet = workbook.getWorksheets().get(0);

    Range sourceRange = worksheet.getCells().createRange(“B3:G9”);

    Workbook targetWorkbook = new Workbook();
    Worksheet targetWs = targetWorkbook.getWorksheets().get(0);

         Range targetRange = targetWs.getCells().createRange("B3:G9");
         
        targetRange.copy(sourceRange);
    
           TxtSaveOptions opts = new TxtSaveOptions(SaveFormat.CSV);
    

    targetWorkbook.save(“out1.csv”, opts);

  2. You should remove all other contents in the sheet except for your desired range, then save the workbook to CSV file format.

Hope, this helps a bit.