Java Code Examples for org.supercsv.io.ICsvBeanWriter#writeHeader()

The following examples show how to use org.supercsv.io.ICsvBeanWriter#writeHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainController.java    From video-streaming-service with MIT License 6 votes vote down vote up
@RequestMapping("download-csv/{id}")
public void downloadCSV(HttpServletResponse response, @PathVariable("id") Long id) throws IOException {
    final Material material = materialRepository.findOne(id);
    if (material == null)
        throw new IllegalArgumentException("[" + id + "] data is not exist.");

    String videoName = material.getVideoName();
    int lastIndexOf = videoName.lastIndexOf("/");
    videoName = lastIndexOf >= 0 ? videoName.substring(lastIndexOf + 1, videoName.length()) : videoName;
    final String csvFileName = videoName + "_" + (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(material.getCreatedAt())) + ".csv";

    response.setContentType("text/csv");

    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", csvFileName);
    response.setHeader(headerKey, headerValue);

    final ICsvBeanWriter csvWriter = new CsvBeanWriter(response.getWriter(), CsvPreference.STANDARD_PREFERENCE);
    final String[] header = {"timestamp", "key"};
    csvWriter.writeHeader(header);

    for (MaterialData data : material.getMaterialDataList())
        csvWriter.write(data, header);

    csvWriter.close();
}
 
Example 2
Source File: ReadWriteCSV.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
public ICsvBeanWriter writeCollectorTweetIDSCSV(ICsvBeanWriter beanWriter, List<Tweet> tweetsList, String collectionDIR, String fileName) {
	try {
		// the header elements are used to map the bean values to each column (names must match)
		//final String[] header = new String[]{"tweetID", "message","userID", "userName", "userURL", "createdAt", "tweetURL"};
		//final CellProcessor[] processors = getProcessors();

		// koushik: shouldn't we be writing only the tweetIDs?
		final String[] header = new String[]{"tweetID"};

		final CellProcessor[] processors = getProcessors4TweetIDSCCSV();

		String persisterDIR = PersisterConfigurator.getInstance().getProperty(PersisterConfigurationProperty.DEFAULT_PERSISTER_FILE_PATH);
		//fileName = StringUtils.substringBefore(fileName, ".json"); //removing .json extension
		String fileToWrite = persisterDIR + collectionDIR + "/" + fileName;
		logger.info(collectionDIR + ": Writing CSV file : " + fileToWrite);

		if (null == beanWriter) { 
			beanWriter = getCSVBeanWriter(fileToWrite);
			// write the header
			beanWriter.writeHeader(header);
		}

		for (final Tweet tweet : tweetsList) {
			try {
				if (tweet.getTweetID() != null) {
					beanWriter.write(tweet, header, processors);
				}
			} catch (SuperCsvCellProcessorException e) {
				logger.error(collectionDIR + ": SuperCSV error");
			}
		}

	} catch (IOException ex) {
		logger.error(collectionDIR + ": IO Exception occured");
	} 
	//return fileName+".csv";
	return beanWriter;
}
 
Example 3
Source File: ReadWriteCSV.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
public ICsvBeanWriter writeCollectorTweetsCSV(List<Tweet> tweetsList, String collectionDIR, String fileName, ICsvBeanWriter beanWriter) {

		try {
			final String[] header = new String[]{"tweetID", "message","userID", "userName", "userURL", "createdAt", "tweetURL"};
			final CellProcessor[] processors = getCollectorTweetsProcessors();

			if(null == beanWriter){
				String persisterDIR = PersisterConfigurator.getInstance().getProperty(PersisterConfigurationProperty.DEFAULT_PERSISTER_FILE_PATH);
				//fileName = StringUtils.substringBefore(fileName, ".json"); //removing .json extension
				String fileToWrite = persisterDIR + collectionDIR + "/" + fileName;
				logger.info(collectionDIR + ": Writing CSV file : " + fileToWrite);
				beanWriter = getCSVBeanWriter(fileToWrite);
				beanWriter.writeHeader(header);
			}

			for (final Tweet tweet : tweetsList) {
				try {
					beanWriter.write(tweet, header, processors);
				} catch (SuperCsvCellProcessorException e) {
					logger.error(collectionDIR + ": SuperCSV error");
				}
			}

		} catch (IOException ex) {
			logger.error(collectionDIR + ": IO Exception occured");
		}
		return beanWriter;
	}