All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dhtmlx.xml2excel.HTMLWriter Maven / Gradle / Ivy

package com.dhtmlx.xml2excel;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

public class HTMLWriter extends BaseWriter{
	int rows = 0;
	int cols = 0;
	int fontSize = -1;
	String watermark = null;

	public void generate(String xml, HttpServletResponse resp) throws IOException {
		CSVxml data = new CSVxml(xml);
		
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/vnd.ms-excel");
		resp.setCharacterEncoding("UTF-8");
		resp.setHeader("Content-Disposition", "attachment;filename=grid.xls");
		resp.setHeader("Cache-Control", "max-age=0");
	
		String[] csv;
		int colsnum = 0;
		PrintWriter writer = resp.getWriter();

		Colors colors = getColors(data);
		startHTML(writer, colors);
		csv = data.getHeader();
		if (csv != null)colsnum = csv.length;
		while(csv != null){			
			writer.append(dataAsString(csv, "header"));
			csv = data.getHeader();
		}
		
		csv = data.getRow();
		if (csv != null){
			colsnum = csv.length;
			cols = csv.length;
		}
		while(csv != null){
			String className = rows%2 == 0 ? "cell_odd" : "cell_even";
			writer.append(dataAsString(csv, className));
			csv = data.getRow();
			rows +=1;
		}
		
		csv = data.getFooter();
		if (csv != null)colsnum = csv.length;
		while(csv != null){			
			writer.append(dataAsString(csv, "footer"));
			writer.flush();
			csv = data.getFooter();
		}
		drawWatermark(writer, colsnum);
		endHTML(writer);
		
		writer.flush();
		writer.close();
	}

	private void drawWatermark(PrintWriter writer, int colsnum) {
		if (watermark != null)
			writer.append("" + watermark + "");
	}

	private void endHTML(PrintWriter writer) {
		writer.append("");
	}

	private void startHTML(PrintWriter writer, Colors colors) {
		writer.append("");
		writer.append("");
		if (fontSize != -1)
			writer.append("");
		writer.append("");
	}


	private String dataAsString(String[] csv, String className) {
		if (csv.length == 0) return "";
		
		StringBuffer buff = new StringBuffer();
		buff.append("");
		for ( int i=0; i");
			buff.append(csv[i].replace("&", "&").replace(">", ">").replace("<", "<"));
			buff.append("");
		}	
		buff.append("\n");
		return buff.toString();
	}

	public int getColsStat() {
		return cols;
	}

	public int getRowsStat() {
		return rows;
	}
	
	public void setFontSize(int fontsize) {
		this.fontSize = fontsize;
	}

	public void setWatermark(String watermark) {
		this.watermark = watermark;
	}
	
	private Colors getColors(CSVxml data) {
		String profile = data.getProfile();
		Colors colors = new Colors();
		colors.setColorProfile(profile);
		return colors;
	}
}