⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceldo.java

📁 java swing源码 欢迎下载 有问题请联系 我一定负责到底
💻 JAVA
字号:
/**
 * Excel操作
 */
package com.NCL.excel;

import java.io.*;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
import jxl.format.Alignment; 
import jxl.format.VerticalAlignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;

public class ExcelDo {
	/**
	 * sheet页头,逗号分割 如:Head = "用户帐号,姓名,密码,激活码,地址,电话,创建日期";
	 * */
	public String Head = "用户帐号,姓名,密码,激活码,地址,电话,创建日期";
	/**
	 * sheet页号
	 * */
	public int SheetNo = 0;
	/**
	 * sheet页名称
	 * */
	public String SheetName = "Sheet1";
	public Pattern p = Pattern.compile(",");
	public String pathExcel;
	public String name;
	public String strList="";
	
	private static final String CONTENT_TYPE = "text/html; charset=GBK";
	public void ExcelDo() throws Exception {
	}
	public SimpleDateFormat  dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	
	private OutputStream os;
	public WritableWorkbook wwb;
	public WritableSheet ws;
	public int headLength;
	/**
	 * 根据路径创建excel表
	 * @param path
	 * @return
	 */
	public boolean setExport(String path){
		try {
			String tPath = path + name;
			pathExcel = tPath;
			//System.out.println(tPath);
			os = new FileOutputStream(pathExcel);
			wwb = Workbook.createWorkbook(os);
			
		} catch (Exception e) {
			System.out.println("DataExport.dbtoexcel():" + e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 设置页头
	 * @param head
	 * @return
	 */
	public boolean setHead(String head){
		try {
			WritableCellFormat   wcf_title =setFormat();
			p = Comma();
			String[] headarray = StringForExcel(head);
			ws = wwb.createSheet(SheetName, SheetNo);
			// 写入标题head
			for (int i = 0; i < headarray.length; i++) {
				Label labelhead = new Label(i, 0, headarray[i],wcf_title);
				ws.setColumnView(i, headarray[i].getBytes().length+10);
				ws.addCell(labelhead);
			}
		} catch (Exception e) {
			System.out.println("DataExport.dbtoexcel():" + e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 设置页头
	 * @param setHeads
	 * @return
	 */
	public boolean setHeadTest(String setHeads){
		try {
			WritableCellFormat   wcf_title =setFormat();
			p = Bathetic();
			String[] headarray = StringForExcel(setHeads);
			ws = wwb.createSheet(SheetName, SheetNo);
			// 写入标题head
			for (int i = 0; i < headarray.length; i++) {
				p = Comma();
				String[] headParticular = StringForExcel(headarray[i]);
				//合并单元格
				ws.mergeCells(Integer.parseInt(headParticular[2])-1, Integer.parseInt(headParticular[1])-1,Integer.parseInt(headParticular[4])-1, Integer.parseInt(headParticular[3])-1);
				ws.setColumnView(Integer.parseInt(headParticular[2])-1, (headParticular[0].getBytes().length+10));
				Label labelhead = new Label(Integer.parseInt(headParticular[2])-1, Integer.parseInt(headParticular[1])-1, headParticular[0],wcf_title);
				ws.addCell(labelhead);
			}
			
		} catch (Exception e) {
			System.out.println("DataExport.dbtoexcel():" + e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 设置单元格格式
	 * @return
	 * @throws WriteException
	 */
	public WritableCellFormat setFormat() throws WriteException{
		//WritableFont   NormalFont   =   new   WritableFont(WritableFont.ARIAL,10);     
		WritableFont   BoldFont   =   new   WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD);  
		WritableCellFormat   wcf_title   =   new   WritableCellFormat(BoldFont);   
		wcf_title.setBorder(Border.NONE,   BorderLineStyle.THIN);   //线条   
		wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE);   //垂直对齐   
		wcf_title.setAlignment(Alignment.CENTRE);   //水平对齐   
		wcf_title.setWrap(false);   //是否换行   

		return wcf_title;
	}
	/**
	 * 往line行row列写入listStr
	 * @param row
	 * @param line
	 * @param listStr
	 * @return
	 */
	public boolean setLabel(int row,int line,String listStr){
		Label label = new Label(row, line, listStr);
		try {
			ws.addCell(label);
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 关闭excel
	 * @return
	 */
	public boolean close(){
		try{
		wwb.write();
		wwb.close();
		if(os!=null)os.close();
		} catch (Exception e) {
			System.out.println("DataExport.dbtoexcel():" + e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 生成随机数字
	 * @param from
	 * @param to
	 * @return
	 */
	private int getRandom(int from, int to) {
		Random random = new Random();
		int r = from + Math.abs(random.nextInt()) % (from - to);
		return r;
	}
	/**
	 * 拆分数据
	 * @param StrTear
	 * @return
	 */
	private String[] StringForExcel(String StrTear){
		String[] pp =p.split(StrTear);
		return pp;
	}
	/**
	 * 设置分隔符号为","
	 * @return
	 */
	private Pattern Comma(){
		return Pattern.compile(",");
	}
	/**
	 * 设置分隔符号为"[|]"
	 * @return
	 */
	private Pattern Bathetic(){
		return Pattern.compile("[|]");
	}
	
	public static void main(String []args){
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -