Example usage for org.apache.commons.csv CSVFormat EXCEL

List of usage examples for org.apache.commons.csv CSVFormat EXCEL

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVFormat EXCEL.

Prototype

CSVFormat EXCEL

To view the source code for org.apache.commons.csv CSVFormat EXCEL.

Click Source Link

Document

Excel file format (using a comma as the value delimiter).

Usage

From source file:org.apache.camel.dataformat.csv.CsvDataFormatTest.java

@Test
public void shouldDefineFormatByName() {
    CsvDataFormat dataFormat = new CsvDataFormat().setFormatName("EXCEL");

    // Properly saved
    assertSame(CSVFormat.EXCEL, dataFormat.getFormat());

    // Properly used
    assertEquals(CSVFormat.EXCEL, dataFormat.getActiveFormat());
}

From source file:org.apache.logging.log4j.core.layout.CsvLogEventLayoutTest.java

@Test
public void testLayoutExcel() throws Exception {
    testLayout(CSVFormat.EXCEL);
}

From source file:org.apache.nifi.csv.CSVUtils.java

public static CSVFormat createCSVFormat(final PropertyContext context) {
    final String formatName = context.getProperty(CSV_FORMAT).getValue();
    if (formatName.equalsIgnoreCase(CUSTOM.getValue())) {
        return buildCustomFormat(context);
    }/*from w  w w .  ja v a2s . com*/
    if (formatName.equalsIgnoreCase(RFC_4180.getValue())) {
        return CSVFormat.RFC4180;
    } else if (formatName.equalsIgnoreCase(EXCEL.getValue())) {
        return CSVFormat.EXCEL;
    } else if (formatName.equalsIgnoreCase(TDF.getValue())) {
        return CSVFormat.TDF;
    } else if (formatName.equalsIgnoreCase(MYSQL.getValue())) {
        return CSVFormat.MYSQL;
    } else if (formatName.equalsIgnoreCase(INFORMIX_UNLOAD.getValue())) {
        return CSVFormat.INFORMIX_UNLOAD;
    } else if (formatName.equalsIgnoreCase(INFORMIX_UNLOAD_CSV.getValue())) {
        return CSVFormat.INFORMIX_UNLOAD_CSV;
    } else {
        return CSVFormat.DEFAULT;
    }
}

From source file:org.apache.nifi.csv.TestJacksonCSVRecordReader.java

@Test
public void testExcelFormat() throws IOException, MalformedRecordException {
    final List<RecordField> fields = new ArrayList<RecordField>();
    fields.add(new RecordField("fieldA", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("fieldB", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final String headerLine = "fieldA,fieldB";
    final String inputRecord = "valueA,valueB";
    final String csvData = headerLine + "\n" + inputRecord;
    final byte[] inputData = csvData.getBytes();

    try (final InputStream bais = new ByteArrayInputStream(inputData);
            final JacksonCSVRecordReader reader = createReader(bais, schema, CSVFormat.EXCEL)) {

        final Object[] record = reader.nextRecord().getValues();
        final Object[] expectedValues = new Object[] { "valueA", "valueB" };
        Assert.assertArrayEquals(expectedValues, record);

        assertNull(reader.nextRecord());
    }// w  w w  . j av a  2 s.  com
}

From source file:org.apache.nifi.processors.ParseCSV.ParseCSV.java

private CSVFormat buildFormat(String format, char delimiter, Boolean with_header, String custom_header) {
    CSVFormat csvFormat = null;//from  w ww  .  j a va 2s. c  om

    // set pre built format
    if (format.equals("DEFAULT")) {
        csvFormat = CSVFormat.DEFAULT;
    } else if (format.equals("EXCEL")) {
        csvFormat = CSVFormat.EXCEL;
    }

    if (with_header & custom_header != null) {
        csvFormat = csvFormat.withSkipHeaderRecord(true);
        csvFormat = csvFormat.withHeader(custom_header);
    } else if (with_header & custom_header == null) {
        csvFormat = csvFormat.withHeader();
    }

    if (delimiter > 0) {
        csvFormat = csvFormat.withDelimiter(delimiter);
    }
    return csvFormat;
}

From source file:org.apache.tika.parser.csv.TextAndCSVParser.java

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {

    CSVParams params = getOverride(metadata);
    Reader reader = null;/*from  w w  w .  ja v  a2 s . c  o  m*/
    Charset charset = null;
    if (!params.isComplete()) {
        reader = detect(params, stream, metadata, context);
        if (params.getCharset() != null) {
            charset = params.getCharset();
        } else {
            charset = ((AutoDetectReader) reader).getCharset();
        }
    } else {
        reader = new BufferedReader(new InputStreamReader(stream, params.getCharset()));
        charset = params.getCharset();
    }

    updateMetadata(params, metadata);

    //if text or a non-csv/tsv category of text
    //treat this as text and be done
    //TODO -- if it was detected as a non-csv subtype of text
    if (!params.getMediaType().getBaseType().equals(CSV) && !params.getMediaType().getBaseType().equals(TSV)) {
        handleText(reader, charset, handler, metadata);
        return;
    }

    CSVFormat csvFormat = CSVFormat.EXCEL.withDelimiter(params.getDelimiter());
    metadata.set(DELIMITER_PROPERTY, CHAR_TO_STRING_DELIMITER_MAP.get(csvFormat.getDelimiter()));

    XHTMLContentHandler xhtmlContentHandler = new XHTMLContentHandler(handler, metadata);
    try (org.apache.commons.csv.CSVParser commonsParser = new org.apache.commons.csv.CSVParser(reader,
            csvFormat)) {
        xhtmlContentHandler.startDocument();
        xhtmlContentHandler.startElement(TABLE);
        try {
            for (CSVRecord row : commonsParser) {
                xhtmlContentHandler.startElement(TR);
                for (String cell : row) {
                    xhtmlContentHandler.startElement(TD);
                    xhtmlContentHandler.characters(cell);
                    xhtmlContentHandler.endElement(TD);
                }
                xhtmlContentHandler.endElement(TR);
            }
        } catch (IllegalStateException e) {
            //if there's a parse exception
            //try to get the rest of the content...treat it as text for now
            //There will be some content lost because of buffering.
            //TODO -- figure out how to improve this
            xhtmlContentHandler.endElement(TABLE);
            xhtmlContentHandler.startElement("div", "name", "after exception");
            handleText(reader, xhtmlContentHandler);
            xhtmlContentHandler.endElement("div");
            xhtmlContentHandler.endDocument();
            //TODO -- consider dumping what's left in the reader as text
            throw new TikaException("exception parsing the csv", e);
        }

        xhtmlContentHandler.endElement(TABLE);
        xhtmlContentHandler.endDocument();
    }
}

From source file:org.bedework.eventreg.bus.CSVOutputter.java

@Override
public String next() {
    if (!regit.hasNext()) {
        return null;
    }/*from   w w  w  .java 2 s .c o  m*/

    /*
    ><c:forEach var="reg" items="${regs}" varStatus="loopStatus"><%--
    </c:forEach>
    */
    final List flds = new ArrayList();

    final Registration reg = regit.next();

    final StringBuilder out = new StringBuilder();

    try {
        final CSVPrinter csv = new CSVPrinter(out, CSVFormat.EXCEL);
        flds.add(reg.getEvSummary());
        flds.add(reg.getEvDate());
        flds.add(reg.getEvTime());
        flds.add(reg.getEvLocation());
        flds.add(reg.getRegistrationId());
        flds.add(reg.getAuthid());
        flds.add(reg.getTicketsRequested());
        flds.add(reg.getNumTickets());
        flds.add(reg.getType());
        flds.add(reg.getComment());
        flds.add(reg.getCreated());
        flds.add(reg.getLastmod());

        if (form == null) {
            csv.printRecord(flds.toArray());
            csv.flush();
            csv.close();
            return out.toString();
        }

        final FormFields ff = new FormFields(form.getFields());

        try {
            final Map vals = reg.restoreFormValues();

            for (final FieldDef fd : ff) {
                final Object val = vals.get(fd.getName());

                if (val == null) {
                    flds.add("");
                } else {
                    flds.add(val);
                }
            }
        } catch (final Throwable t) {
            out.append("Exception restoring form values");
        }

        csv.printRecord(flds.toArray());
        csv.flush();
        csv.close();
    } catch (final Throwable t) {
        return "Exception " + t.getLocalizedMessage();
    }

    return out.toString();
}

From source file:org.cast.cwm.admin.CSVDownload.java

/**
 * creates a new resource response based on the request attributes
 *
 * @param attributes current request attributes from client
 * @return resource response for answering request
 *//*from   www  .j  a  v  a 2  s  .c o m*/
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    ResourceResponse rr = new ResourceResponse();
    rr.disableCaching();
    rr.setFileName("log.csv");
    rr.setContentDisposition(ContentDisposition.ATTACHMENT);
    rr.setContentType("text/csv");

    if (rr.dataNeedsToBeWritten(attributes)) {
        rr.setWriteCallback(new WriteCallback() {
            @Override
            public void writeData(Attributes attributes) {
                Response response = attributes.getResponse();

                try {
                    CSVPrinter writer = new CSVPrinter(
                            new OutputStreamWriter(response.getOutputStream(), "UTF-8"), CSVFormat.EXCEL);

                    // Write header row
                    for (IDataColumn<E> col : columns) {
                        writer.print(col.getHeaderString());
                    }
                    writer.println();

                    // Write documentation row, if requested
                    if (includeDocumentationRow) {
                        for (IDataColumn<E> col : columns) {
                            if (col instanceof IDocumentedColumn
                                    && ((IDocumentedColumn) col).getDocumentationModel() != null) {
                                writer.print(((IDocumentedColumn) col).getDocumentationModel().getObject());
                            } else {
                                writer.print("");
                            }
                        }
                        writer.println();
                    }

                    // Write Data
                    Iterator<? extends E> it = iteratorProvider.getIterator();
                    while (it.hasNext()) {
                        E e = it.next();
                        for (IDataColumn<E> col : columns) {
                            String columnValue = col.getItemString(new Model<E>(e));
                            if (columnValue == null) {
                                log.warn("Got a null value for {} of item {}", col.getHeaderString(), e);
                                columnValue = "null";
                            }
                            // Clean up text -- CSV file cannot have newlines in it
                            writer.print(columnValue.replaceAll("[\r\n]", " "));
                        }
                        writer.println();
                    }
                    writer.close();

                } catch (UnsupportedEncodingException e) {
                    throw new StringValueConversionException("UTF-8 translation not supported?!", e);
                } catch (IOException e) {
                    throw new WicketRuntimeException("Couldn't write to resource", e);
                }
            }
        });
    }

    return rr;
}

From source file:org.cast.cwm.service.UserSpreadsheetReader.java

/**
 * Read spreadsheet of user information and generate potential users.
 * Returns true if all was sucessful and users could be created as specified.
 * /*from  w w w  .j av a  2  s  .  c  o m*/
 * This method does NOT modify the datastore.
 * 
 * @param stream the input stream of CSV data
 * @return true if no errors encountered.
 */
@Override
public boolean readInput(InputStream stream) {
    potentialUsers = new ArrayList<PotentialUserSave>();
    potentialSites = new HashMap<String, Site>();
    potentialPeriods = new HashMap<Site, Map<String, Period>>();

    CSVParser parser;
    try {
        parser = CSVFormat.EXCEL.withHeader().withIgnoreEmptyLines().withIgnoreSurroundingSpaces()
                .parse(new InputStreamReader(new BOMInputStream(stream), "UTF-8"));
    } catch (IOException e) {
        log.error(e.getMessage());
        globalError = e.getMessage();
        return false;
    }

    // Make our own secondary mapping of header names to fields, by
    // lowercasing and removing spaces from all header names
    headerMap = parser.getHeaderMap();
    for (String hdr : new HashSet<String>(headerMap.keySet())) {
        String normalized = hdr.toLowerCase().replaceAll("\\s", "");
        if (!normalized.equals(hdr)) {
            headerMap.put(normalized, headerMap.get(hdr));
        }
    }

    globalError = checkRequiredHeaders(headerMap);
    if (!Strings.isEmpty(globalError))
        return false;

    // Read the CSV file, create PotentialUserSave objects, record error messages, add to potentialUsers List
    try {
        boolean errors = false; // have errors been encountered?
        for (CSVRecord record : parser) {

            try {
                User user = createUserObject(record);
                String messages = populateUserObject(user, record);
                if (Strings.isEmpty(messages))
                    messages = validateUser(user);

                // Add a PotentialUserSave to the list.
                potentialUsers.add(new PotentialUserSave(modelProvider.modelOf(user), messages, record));
                if (!Strings.isEmpty(messages))
                    errors = true;

            } catch (ArrayIndexOutOfBoundsException e) {
                // This can happen if the last row is missing values; Excel doesn't fill them out to the last column
                log.error("Caught exception importing line {}: {}", parser.getCurrentLineNumber(),
                        e.getClass());
                potentialUsers.add(new PotentialUserSave(null, "Data missing from CSV.\n", record));
                errors = true;
            } catch (Exception e) {
                e.printStackTrace();
                log.error("Caught exception importing line {}: {}", parser.getCurrentLineNumber(),
                        e.getClass());
                potentialUsers.add(new PotentialUserSave(null, "Error: " + e, record));
                errors = true;
            }
        }

        // If CSV file has only one line, it is either empty or has unrecognized LF/CR values.
        if (parser.getCurrentLineNumber() == 1) {
            potentialUsers.add(
                    new PotentialUserSave(null, "Empty or Corrupted File.  Note: Save as Windows CSV.", null));
            globalError = "Empty or Corrupted File - LF/CR values may be invalid!";
            throw new CharacterCodingException();
        }
        return (!errors);

    } catch (CharacterCodingException e) {
        log.error("Empty or Corrupted File - only 1 line found - CR/LF issue?. {}", e.getClass());
        return false;
    }

}

From source file:org.cloudsimulator.controller.PlacementSimulatorTestRunner.java

protected void exportSimulationResults(String dcName, String testCase,
        ArrayList<SimulationResultWrapper> simulationResultWrappers) {

    Path resultPath = Paths.get(this.autoExportResultPath, dcName);

    if (!Files.exists(resultPath)) {
        File resultFile = new File(resultPath.toString());
        resultFile.mkdirs();/*from  w  w  w.ja  v  a 2s .  co m*/
    }

    ArrayList<PlacementResultExt> resultExts = new ArrayList<PlacementResultExt>();

    Path resultFilePath = Paths.get(resultPath.toString(), testCase + ".csv");

    CSVFormat csvFormat = CSVFormat.EXCEL.withDelimiter(';');

    Object[] header = { "Id", "Reserved OS resource", "Max risk", "Max overprov time", "N runs for GRASP",
            "N simulation", "N VM", "N VM used for keep test consistency", "Iteration time", "Euristic method",
            "Euristic coefficient", "N Used host", "CPU Avg", "Mem Avg", "Overall Avg", "Cpu in range",
            "Mem in range", "Overall in range" };

    FileWriter buffer;
    try {
        buffer = new FileWriter(resultFilePath.toString());
        CSVPrinter csvPrinter = new CSVPrinter(buffer, csvFormat);

        csvPrinter.printRecord(header);
        for (SimulationResultWrapper simulationResultWrapper : simulationResultWrappers) {

            for (PlacementResult result : simulationResultWrapper.getPlacementResults()) {
                List record = new ArrayList();
                record.add(result.getId());
                record.add(simulationResultWrapper.getReservedOsHostResource());
                record.add(simulationResultWrapper.getHostMaxRisk());
                record.add(simulationResultWrapper.getMaxOverprovisioningTime());
                record.add(simulationResultWrapper.getN_runs());
                record.add(simulationResultWrapper.getN_simulation());
                record.add(simulationResultWrapper.getOriginalMachines().size());
                record.add(simulationResultWrapper.getUsedForSimulationMachines().size());
                record.add(result.getIterationTime());
                record.add(simulationResultWrapper.getEuristicMethod());
                record.add(simulationResultWrapper.getEuristicCoeffBuilderMethod());
                record.add(result.getUsedHost().size());
                record.add((float) result.getDataCenterMachinePlacement().getCpu_avg_usage());
                record.add((float) result.getDataCenterMachinePlacement().getMemory_avg_usage());
                record.add((float) result.getDataCenterMachinePlacement().getOverall_avg_usage());
                record.add((float) result.getDataCenterMachinePlacement().getCpu_in_range());
                record.add((float) result.getDataCenterMachinePlacement().getMemory_in_range());
                record.add((float) result.getDataCenterMachinePlacement().getOverall_in_range());

                csvPrinter.printRecord(record);
                csvPrinter.flush();

                //            resultExts.add(new PlacementResultExt(result.getId(),
                //                    simulationResultWrapper.getReservedOsHostResource(),
                //                    simulationResultWrapper.getHostMaxRisk(),
                //                    simulationResultWrapper.getMaxOverprovisioningTime(),
                //                    simulationResultWrapper.getN_runs(),
                //                    simulationResultWrapper.getN_simulation(),
                //                    simulationResultWrapper.getOriginalMachines().size(),
                //                    simulationResultWrapper.getUsedForSimulationMachines().size(),
                //                    result.getIterationTime(),
                //                    simulationResultWrapper.getEuristicMethod(),
                //                    simulationResultWrapper.getEuristicCoeffBuilderMethod(),
                //                    result.getUsedHost().size(),
                //                    (float)result.getDataCenterMachinePlacement().getCpu_avg_usage(),
                //                    (float)result.getDataCenterMachinePlacement().getMemory_avg_usage(),
                //                    (float)result.getDataCenterMachinePlacement().getOverall_avg_usage(),
                //                    (float)result.getDataCenterMachinePlacement().getCpu_in_range(),
                //                    (float)result.getDataCenterMachinePlacement().getMemory_in_range(),
                //                    (float)result.getDataCenterMachinePlacement().getOverall_in_range()));
            }
        }
        csvPrinter.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    //            for (PlacementResultExt res : resultExts) {
    //                csvPrinter.printRecords(res);    
    //            }
    //            

}