Working with DSV
This demonstration shows and explains all necessary moments and options regarding processing DSV spreadsheets (Delimiter-separated values)
DSV (Delimiter-Separated Values) documents are specific form of text-based spreadsheets with delimiters (separators). Due of their nature the GroupDocs.Editor processes this class of documents separately from usual binary spreadsheets. In counterpart to usual spreadsheets, DSV documents due to their textual nature have only single tab (worksheet) and cannot be encoded. But any non-empty string may be treated as separator, so user always need to specify it explicitly.
The most common types of DSV are:
- CSV (Comma-Separated value)
- TSV (Tab-Separated value)
- Semicolon-separated value
- Whitespace-separated value
- …and any other
GroupDocs.Editor supports DSV with any separator, which can be character or a set of characters (string).
Loading DSV documents
Unlike WordProcessing and Spreadsheet documents, DSV documents are loaded into the Editor class without any loading options. They are simple text files by their nature, so it is nothing to adjust:
String inputCsvPath = "C://input//spreadsheet.csv"; Editor editor = new Editor(inputCsvPath);
Editing DSV documents
In order to open any DSV document for edit, user must use DelimitedTextEditOptions class, which single constructor has one mandatory parameter — string separator (delimiter), that should not be NULL or empty string. There are also several optional properties. Two properties — ConvertDateTimeData and ConvertNumericData — are boolean flags, which indicate how to treat numbers. GroupDocs.Editor can recognize digits within cells and treat them as numbers or date-time values. By default this recognition is disabled, but user can turn it on. Next property — TreatConsecutiveDelimitersAsOne, — is also a boolean flag, which determines how the consecutive delimiters should be treated — as several (default - false) or as a single one (true).
Last property — OptimizeMemoryUsage, — is also a boolean flag, but with completely different purpose. By default GroupDocs.Editor algorithms are tuned for maximum performance for reducing the latency time; memory consumption has lower priority. However in some rare cases user may need to load and open very huge DSV, several hundreds of MiBs or even close to GiB. In such cases GroupDocs.Editor (Java in fact) may throw an OutOfMemoryException. For coping with such use-cases the OptimizeMemoryUsage flag was introduced. By enabling it user switches GroupDocs.Editor to use another processing algorithms, which consume relatively low amount of memory at the cost of lower performance.
Example below demonstrates using the DelimitedTextEditOptions for editing CSV document, where comma character is a delimiter:
DelimitedTextEditOptions editOptions = new DelimitedTextEditOptions(","); editOptions.setConvertDateTimeData(false); editOptions.setConvertNumericData(true); editOptions.setTreatConsecutiveDelimitersAsOne(true); editOptions.setOptimizeMemoryUsage(true); EditableDocument document = editor.edit(editOptions);
Saving DSV documents
After being edited, input DSV can be saved back to DSV (not necessary with the same separator) or to any supportable Spreadsheet document. In order to save document to DSV format user must use the DelimitedTextSaveOptions class, which, like the DelimitedTextEditOptions, has one constructor with mandatory string parameter — separator (delimiter), that should not be NULL or empty string.
There are also other properties:
- Encoding. Allows to specify the encoding of generated DSV. By default, if not specified, is UTF8.
- TrimLeadingBlankRowAndColumn. Boolean flag, that indicates whether leading blank rows and columns should be trimmed like what MS Excel does.
- KeepSeparatorsForBlankRow. Boolean flag, that indicates whether separators should be output for blank row. Default value is false which means the content for blank row will be empty.
Example below demonstrates loading CSV, opening it to the EditableDocument instance, and saving to TSV and XLSM.
String inputCsvPath = "C://input//spreadsheet.csv"; Editor editor = new Editor(inputCsvPath); DelimitedTextEditOptions editOptions = new DelimitedTextEditOptions(","); EditableDocument document = editor.edit(editOptions); DelimitedTextSaveOptions tsvSaveOptions = new DelimitedTextSaveOptions("\t"); tsvSaveOptions.setTrimLeadingBlankRowAndColumn(true); tsvSaveOptions.setKeepSeparatorsForBlankRow(false); SpreadsheetSaveOptions xlsmSaveOptions = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsm); String tsvSavePath = "C://output//spreadsheet.tsv"; String xlsmSavePath = "C://output//spreadsheet.xlsm"; editor.save(document, tsvSavePath, tsvSaveOptions); editor.save(document, xlsmSavePath, xlsmSaveOptions);
In this example output “‘spreadsheet.tsv” will have a UTF-8 encoding.
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Editor for .NET examples, plugins, and showcase
- GroupDocs.Editor for Java examples, plugins, and showcase
- Document Editor for .NET MVC UI Example
- Document Editor for .NET App WebForms UI Modern Example
- Document Editor for Java App Dropwizard UI Modern Example
- Document Editor for Java Spring UI Example
Free Online App
Along with full-featured Java library we provide simple but powerful free Apps.
You are welcome to edit your Microsoft Word (DOC, DOCX, RTF etc.), Microsoft Excel (XLS, XLSX, CSV etc.), Open Document (ODT, OTT, ODS) and other documents with free to use online GroupDocs Editor App.