java读写excel

java应用jxl API读写excel:

package tf.excel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.CellFormat;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.DateFormats;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import common.Logger;

public class JxlTest {

	private static Logger log = Logger.getLogger(JxlTest.class);

	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	private File file;

	public JxlTest(String fileName) {
		this.file = new File(fileName);
	}

	public JxlTest(File file) {
		this.file = file;
	}

	public static void main(String[] args) {
		JxlTest jxl = new JxlTest("users.xls");
		jxl.writeExcel();

		List<User> list = jxl.readExcel();
		for (User u : list) {
			log.info(u.yhm + "--" + u.xm + "--" + u.age + "--"
					+ df.format(u.sr));
		}
	}

	public void writeExcel() {
		WritableWorkbook ww;
		try {
			ww = Workbook.createWorkbook(file);
			WritableSheet ws = ww.createSheet("用户信息", 0);

			Operate.addLabelToSheet(ws, 0, 0, 3, 0, "用户信息", Style
					.getHeaderStyle());
			Operate.addLabelToSheet(ws, 0, 1, "用户名", Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 1, 1, "姓名", Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 2, 1, "年龄", Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 3, 1, "生日", Style.getTitleStyle());

			Operate.addLabelToSheet(ws, 0, 2, "tt", Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 1, 2, "糖糖", Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 2, 2, 25, Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 3, 2, df.parse("1984-08-12"), Style
					.getDateStyle());
			Operate.addLabelToSheet(ws, 0, 3, "ff", Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 1, 3, "飞飞", Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 2, 3, 24, Style.getTitleStyle());
			Operate.addLabelToSheet(ws, 3, 3, df.parse("1985-11-25"), Style
					.getDateStyle());

			for (int i = 0; i < 4; i++) {
				ws.setColumnView(i, 16);
			}
			ws.setRowView(0, 20);
			ww.write();
			ww.close();
			log.info("写入excel成功!");
		} catch (Exception e) {
			log.info("写入excel失败!");
			e.printStackTrace();
		}

	}

	public List<User> readExcel() {
		List<User> list = new ArrayList<User>();
		Workbook wb;
		try {
			wb = Workbook.getWorkbook(file);

			Sheet sheet = wb.getSheet(0);
			int row = sheet.getRows();
			int col = sheet.getColumns();
			for (int i = 2; i < row; i++) {
				User user = new User();
				for (int j = 0; j < col; j++) {
					Cell cell = sheet.getCell(j, i);

					if (cell.getType() == CellType.LABEL) {
						LabelCell lc = (LabelCell) cell;
						switch (j) {
						case 0:
							user.setYhm(lc.getContents());
						case 1:
							user.setXm(lc.getContents());
						}
					}

					if (cell.getType() == CellType.NUMBER) {
						NumberCell nc = (NumberCell) cell;
						user.setAge((int) nc.getValue());
					}

					if (cell.getType() == CellType.DATE) {
						DateCell dc = (DateCell) cell;
						user.setSr(dc.getDate());
					}
				}
				list.add(user);
			}
			log.info("读取excel成功!");
		} catch (Exception e) {
			log.info("读取excel失敗!");
			e.printStackTrace();
		}
		return list;

	}

	static class Style {
		/**
		 * 页头样式
		 */
		public static WritableCellFormat getHeaderStyle() throws Exception {
			WritableFont font = new WritableFont(WritableFont.TIMES, 14); // 设置14号字体
			font.setColour(Colour.BLUE); // 设置字体颜色
			font.setBoldStyle(WritableFont.BOLD); // 设置粗体字
			WritableCellFormat format = new WritableCellFormat(font); // 设置单元格样式
			format.setBackground(Colour.LIGHT_GREEN); // 设置背景颜色
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);//设置边界样式
			format.setAlignment(Alignment.CENTRE); // 设置水平位置
			format.setVerticalAlignment(VerticalAlignment.CENTRE); // 设置竖直位置
			return format;
		}

		/**
		 * 标题样式
		 */
		public static WritableCellFormat getTitleStyle() throws Exception {
			WritableFont font = new WritableFont(WritableFont.TIMES, 14);
			font.setColour(Colour.GREEN);
			WritableCellFormat format = new WritableCellFormat(font);
			format.setBackground(Colour.VERY_LIGHT_YELLOW);
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			format.setAlignment(Alignment.CENTRE);
			format.setVerticalAlignment(VerticalAlignment.CENTRE);
			return format;
		}

		/**
		 * 内容样式
		 */
		public static WritableCellFormat getContentStyle() throws Exception {
			WritableFont font = new WritableFont(WritableFont.TIMES, 14);
			font.setColour(Colour.BLUE);
			WritableCellFormat format = new WritableCellFormat(font);
			format.setBackground(Colour.VERY_LIGHT_YELLOW);
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			format.setAlignment(Alignment.CENTRE);
			format.setVerticalAlignment(VerticalAlignment.CENTRE);
			return format;
		}

		/**
		 * 普通样式
		 */
		public static WritableCellFormat getNormalStyle() throws Exception {
			WritableFont font = new WritableFont(WritableFont.TIMES, 14);
			font.setColour(Colour.BLUE);
			WritableCellFormat format = new WritableCellFormat(
					DateFormats.FORMAT12);
			format.setBackground(Colour.VERY_LIGHT_YELLOW);
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			format.setAlignment(Alignment.CENTRE);
			format.setVerticalAlignment(VerticalAlignment.CENTRE);
			return format;
		}

		/**
		 * 日期样式
		 */
		public static WritableCellFormat getDateStyle() throws Exception {
			WritableFont font = new WritableFont(WritableFont.TIMES, 14);
			font.setColour(Colour.BLUE);
			WritableCellFormat format = new WritableCellFormat(
					DateFormats.FORMAT1);
			format.setBackground(Colour.VERY_LIGHT_YELLOW);
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			format.setAlignment(Alignment.CENTRE);
			format.setVerticalAlignment(VerticalAlignment.CENTRE);
			return format;
		}
	}

	static class Operate {
		/**
		 * 写入文本
		 */
		public static void addLabelToSheet(WritableSheet ws, int col, int row,
				String cont, CellFormat format) throws Exception {
			Label label = new Label(col, row, cont, format);
			ws.addCell(label);
		}

		/**
		 * 写入文本(合并后的单元格)
		 */
		public static void addLabelToSheet(WritableSheet ws, int firstCol,
				int firstRow, int lastCol, int lastRow, String cont,
				CellFormat format) throws Exception {
			ws.mergeCells(firstCol, firstRow, lastCol, lastRow);
			addLabelToSheet(ws, firstCol, firstRow, cont, format);
		}

		/**
		 * 写入数字
		 */
		public static void addLabelToSheet(WritableSheet ws, int col, int row,
				double num, CellFormat format) throws Exception {
			jxl.write.Number number = new jxl.write.Number(col, row, num,
					format);
			ws.addCell(number);
		}

		/**
		 * 写入日期
		 */
		public static void addLabelToSheet(WritableSheet ws, int col, int row,
				Date date, CellFormat format) throws Exception {

			DateTime dt = new DateTime(col, row, date, format, DateTime.GMT);
			ws.addCell(dt);
		}
	}

	static class User {
		private String yhm;
		private String xm;
		private int age;
		private Date sr;

		public String getYhm() {
			return yhm;
		}

		public void setYhm(String yhm) {
			this.yhm = yhm;
		}

		public String getXm() {
			return xm;
		}

		public void setXm(String xm) {
			this.xm = xm;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public Date getSr() {
			return sr;
		}

		public void setSr(Date sr) {
			this.sr = sr;
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值