jxl创建Excel文件java代码示例

记得要下载 并 导入jxl.jar 包,免积分下载地址:http://download.csdn.net/detail/u010011052/7561041

 

package Test;

import java.io.*;

import jxl.*;
import jxl.format.Colour;
import jxl.write.*;


public class JXLTest {

	private static WritableWorkbook book;
	private static WritableSheet sheet ;
	private static WritableFont normalFont;

	private static WritableFont diffFont;
	private static WritableCellFormat normalFormat;
	private static WritableCellFormat diffFormat;
	
	/**
	 * java创建excel简单示例
	 */
	public static void main(String args[]) {
		createExcel();
	}

	public static void createExcel(){
		try {
			String fileNameAndPath = "E:\\DifferentData\\java创建excel文件示例.xls";
			book = Workbook.createWorkbook(new File(fileNameAndPath));
			// 生成名为"第一页"的工作表,参数0表示这是第一页
			sheet = book.createSheet("第一页", 0);
			// 设置字体为宋体,11号字,不加粗,颜色为红色
			normalFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD);
			// 设置字体为宋体,11号字,不加粗,颜色为红色
			diffFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD);
			diffFont.setColour(Colour.RED);

			normalFormat = new WritableCellFormat(normalFont);
			normalFormat.setAlignment(jxl.format.Alignment.CENTRE);
			normalFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			
			diffFormat = new WritableCellFormat(diffFont);
			diffFormat.setAlignment(jxl.format.Alignment.CENTRE);
			diffFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

			Label labelA = new Label(0, 0, "第一列标题", normalFormat);
			Label labelB = new Label(1, 0, "第二列标题", normalFormat);
			Label labelC = new Label(2, 0, "第三列标题", normalFormat);
			Label labelD = new Label(3, 0, "第四列标题", normalFormat);
			for(int i=1; i<=10; i++){
				Label lab1 = new Label(0,i,"第"+i+"行第1列");
				Label lab2 = new Label(2,i,"第"+i+"行第2列");
				Label lab3 = new Label(3,i,"第"+i+"行第3列",diffFormat);
				Label lab4 = new Label(4,i,"第"+i+"行第4列");
				sheet.addCell(lab1);
				sheet.addCell(lab2);
				sheet.addCell(lab3);
				sheet.addCell(lab4);
			}
			// 将定义好的单元格添加到工作表中
			sheet.addCell(labelA);
			sheet.addCell(labelB);
			sheet.addCell(labelC);
			sheet.addCell(labelD);
			
			book.write();
			book.close();
			System.out.println("创建文件成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
		}
	}
	
}


 读取csv文件

File csv = new File("E:\\江苏省四维POI.csv"); // CSV文件
			BufferedReader br = new BufferedReader(new FileReader(csv));
			//for(int num = 1; num<=rowMaxNum; num++){
			String lineText = "";
			// 读取直到最后一行
			int i=0;
			while ((lineText = br.readLine()) != null && i<=200) {
				if(i>0){
					String arr[] = lineText.split(",");
					String PNNAME = arr[0];
					String POINT_X = arr[1];
					String POINT_Y = arr[2];
					String POIID = arr[3];
					findDifferent(PNNAME,POINT_X,POINT_Y,POIID);
				}
				i++;
			}
			br.close();


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值