java uses jxl.jar to generate excel documents

java code:
package com.test.read;

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * write excel
 *
 * @author user
 *
 */
public class writeExcel {
	public void writeExc(File filename) {
		WritableWorkbook wwb = null;
		try {
			wwb = Workbook.createWorkbook(filename);
		} catch (Exception e) {
			e.printStackTrace ();
		}

		// Create Excel worksheet
		WritableSheet ws = wwb.createSheet("Address Book", 0);// Create sheet
		try {
			ws.mergeCells(0, 0, 2, 1);// Merge cells (left column, left row, right column, right row) from row 1, column 1 to row 2, column 3
			Label header = new Label(0, 0, "Address Book (Class 191026)", getHeader());
			ws.addCell(header);//Write the header
			Label l = new Label(0, 2, "Name", getTitle());// Line 3
			ws.addCell(l);
			l = new Label(1, 2, "Phone", getTitle());
			ws.addCell(l);
			l = new Label(2, 2, "Address", getTitle());
			ws.addCell(l);
			l = new Label(0, 3, "Little Wish", getNormolCell());// Line 4
			ws.addCell(l);
			l = new Label(1, 3, "1314***0974", getNormolCell());
			ws.addCell(l);
			l = new Label(2, 3, "Wuchang, Wuhan", getNormolCell());
			ws.addCell(l);
			l = new Label(0, 4, "Xiao Shi", getNormolCell());// Line 5
			ws.addCell(l);
			l = new Label(1, 4, "1347***5057", getNormolCell());
			ws.addCell(l);
			l = new Label(2, 4, "Wuchang, Wuhan", getNormolCell());
			ws.addCell(l);
			ws.setColumnView(0, 20);// Set the column width
			ws.setColumnView(1, 20);
			ws.setColumnView(2, 40);
			ws.setRowView(0, 400);// Set the row height
			ws.setRowView (1,400);
			ws.setRowView (2, 500);
			ws.setRowView (3,500);
			ws.setRowView (4,500);
		} catch (RowsExceededException e1) {
			e1.printStackTrace();
		} catch (WriteException e1) {
			e1.printStackTrace();
		}

		// output stream
		try {
			wwb.write();
		} catch (IOException ex) {

			ex.printStackTrace();
		}
		// close the stream
		try {
			wwb.close();
		} catch (WriteException ex) {

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

			ex.printStackTrace();
		}
		// outStream.close();
		System.out.println("Write successful!\n");
	}

	public void readExc(File filename) throws BiffException, IOException {
		Workbook wb = Workbook.getWorkbook(filename);
		Sheet s = wb.getSheet(0);// 第1个sheet
		Cell c = null;
		int row = s.getRows();// total number of rows
		int col = s.getColumns();// total number of columns
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				c = s.getCell(j, i);
				System.out.print(c.getContents() + "  ");
			}
			System.out.println();
		}
	}

	/**
	 * Set the style of the header
	 *
	 * @return
	 */
	public static WritableCellFormat getHeader() {
		WritableFont font = new WritableFont(WritableFont.TIMES, 24,
				WritableFont.BOLD);// Define the font
		try {
			font.setColour(Colour.BLUE);// blue font
		} catch (WriteException e1) {

			e1.printStackTrace();
		}
		WritableCellFormat format = new WritableCellFormat(font);
		try {
			format.setAlignment (jxl.format.Alignment.CENTRE); // 左右 居中
			format.setVerticalAlignment (jxl.format.VerticalAlignment.CENTRE); // 上下 居中
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);// black border
			format.setBackground(Colour.YELLOW);// yellow background
		} catch (WriteException e) {

			e.printStackTrace ();
		}
		return format;
	}

	/**
	 * Set the title style
	 *
	 * @return
	 */
	public static WritableCellFormat getTitle() {
		WritableFont font = new WritableFont(WritableFont.TIMES, 14);
		try {
			font.setColour(Colour.BLUE);// blue font
		} catch (WriteException e1) {

			e1.printStackTrace();
		}
		WritableCellFormat format = new WritableCellFormat(font);

		try {
			format.setAlignment (jxl.format.Alignment.CENTRE);
			format.setVerticalAlignment (jxl.format.VerticalAlignment.CENTRE);
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
		} catch (WriteException e) {

			e.printStackTrace ();
		}
		return format;
	}

	/**
	 * Set other cell styles
	 *
	 * @return
	 */
	public static WritableCellFormat getNormolCell() {
		// font size 12, centered up and down, left and right, with black border
		WritableFont font = new WritableFont(WritableFont.TIMES, 12);
		WritableCellFormat format = new WritableCellFormat(font);
		try {
			format.setAlignment (jxl.format.Alignment.CENTRE);
			format.setVerticalAlignment (jxl.format.VerticalAlignment.CENTRE);
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
		} catch (WriteException e) {

			e.printStackTrace ();
		}
		return format;
	}

	public static void main(String[] args) throws IOException, BiffException {
		writeExcel wr = new writeExcel();
		File f = new File("D:\\Users\\address.xls");
		f.createNewFile();
		wr.writeExc(f);
		wr.readExc(f);
	}
}





result:







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326441468&siteId=291194637