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

📄 copyeditexcel.java

📁 基于Java的地图数据管理软件。使用MySQL数据库管理系统。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.aetherial.gis.excel;

import java.io.*;
import jxl.*;
import jxl.write.*;
import java.util.*;
import jxl.format.*;
import java.text.SimpleDateFormat;

public class CopyEditExcel{
  private String tracksName ="";
  private String sheetName = "", filePath = "";
  private jxl.write.WritableWorkbook wwb = null;
  private jxl.write.WritableCell wc = null;
  private String[] number = null, name = null, lat = null, lon = null;
  private String[] numberTemp = null, nameTemp = null, latTemp = null, lonTemp = null;
  private int page = 1, totalPage = 0;
  private static String waypointOfBegin="",waypointOfEnd,disctance="";
  private boolean firstPage = true;
  private WritableCellFormat wcf =null;
  private String cellNumber = "",cellName ="", cellType ="",cellWidth ="",t1="",t2="",t3="",t4="",t5="",t6="",t7="";
  public CopyEditExcel() {
  }

  public CopyEditExcel(String filePath) throws Exception{

    this.number = new String[40];
    this.name = new String[40];
    this.lat = new String[40];
    this.lon = new String[40];

    this.filePath = filePath;
    //synchronized(this) {
    try {
      wwb = Workbook.createWorkbook(new File(
          filePath), this.readStandard("standard1.xls"));

    }
    catch (FileNotFoundException e) {
      System.out.print(e);
    }catch(java.io.IOException e){

    }
    //}
  }

  private void setNumber(String[] number) {
    this.number = number;
  }

  private void setName(String[] name) {
    this.name = name;
  }

  private void setLatAndLon(String[] Latitude, String[] Longitude) {
    this.lat = Latitude;
    this.lon = Longitude;
  }

  private Workbook readStandard(String filePath) {
    Workbook wb = null;
    try {
      wb = Workbook.getWorkbook(new File(filePath));
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return wb;
  }

  private void setWcContent(String str, WritableSheet ws) {
    if (wc.getType() == CellType.LABEL) {
      wcf = new WritableCellFormat();
      try {
        wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
        wcf.setAlignment(jxl.format.Alignment.CENTRE);
        wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
        jxl.write.Label label = new jxl.write.Label(wc.getColumn(),
            wc.getRow(), str, wcf);
        ws.addCell(label);
      }
      catch (Exception e) {
        e.printStackTrace();
        //System.out.print("Here is 79 line ");
      }
    }
    else if (wc.getType() == CellType.NUMBER) {
      wcf = new WritableCellFormat();
      try {
        wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
        jxl.write.Number number = new jxl.write.Number(wc.getColumn(),
            wc.getRow(), Double.parseDouble(str), wcf);
        ws.addCell(number);
      }
      catch (Exception e) {
        e.printStackTrace();
        //System.out.print("Here is 79 line ");
      }
    }

  }
  /*
  在这里自己来定义边框。
  */
 private void setBorderOK(WritableSheet ws,WritableFont wf,jxl.format.Alignment align,String contents,boolean rightBorder){
   //wc = ws.getWritableCell(0,2);
   String str = contents;
   if (str.equals("")){
     str = " ";
   }
   wcf = new WritableCellFormat(wf);
   try{
     wcf.setBorder(jxl.format.Border.TOP, jxl.format.BorderLineStyle.THIN);
     if(rightBorder){
       wcf.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.THIN);
     }
     wcf.setBorder(jxl.format.Border.BOTTOM, jxl.format.BorderLineStyle.THIN);
     if(wc.getColumn()==0){
       wcf.setBorder(jxl.format.Border.LEFT, jxl.format.BorderLineStyle.THIN);
     }
     wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
     wcf.setAlignment(align);
     jxl.write.Label label = new jxl.write.Label(wc.getColumn(),
            wc.getRow(), str, wcf);
        ws.addCell(label);
   }catch(Exception e){
     e.printStackTrace();
   }
 }
  /*
   在这里,1、先给出4个下标为80的有值数组,并赋值给全局变量,并定义WritableSheet ws的工作表。
          2、调用createFile(WritableSheet ws),这里将把数组的内容写到这个工作表中
          3、继续给全局变量数组赋值,继续调用createFile(WritableSheet ws),
          4、完毕之后,调用wwbClose(),关闭所有操作。
   */
  private void createFile(WritableSheet ws) {
    try {
      //jxl.write.WritableSheet ws = wwb.getSheet(0);
      //wwb.copySheet(0,"testtest",1);
      //jxl.write.WritableSheet ws1 = wwb.getSheet(1);
      ws.setName("Page" + page);
      ExcelStandard es = new ExcelStandard();
      int[] stand = es.standardNumber(10);
      //定义日期
      SimpleDateFormat bartDateFormat =
          new SimpleDateFormat("yyyy-MM-dd");
      Date date = new Date();
      String thisDay = bartDateFormat.format(date);

      wc = ws.getWritableCell(10, 1);
      Label l = (Label) wc;
      l.setString("道路类型:已建        编制日期:   " +
                  thisDay.substring(0, 4) + "年 " + thisDay.substring(5, 7) +
                  "月 " + thisDay.substring(8, 10) + "日");
      /*
      wc = ws.getWritableCell(0,5);
      Label l1 = (Label) wc;
      l1.setString(this.cellNumber);

      wc = ws.getWritableCell(1,5);
      Label l2 = (Label) wc;
      l2.setString(this.cellName);

      wc = ws.getWritableCell(9,5);
      Label l3 = (Label) wc;
      l3.setString(this.cellType);

      wc = ws.getWritableCell(11,5);
      Label l4 = (Label) wc;
      l4.setString(this.cellWidth);
*/

      WritableFont wf = new WritableFont(WritableFont.ARIAL,
                                         12,
                                         WritableFont.NO_BOLD,
                                         false,
                                         UnderlineStyle.NO_UNDERLINE
                                         );
      this.setBorderOK(ws,wf,jxl.format.Alignment.LEFT,wc.getContents(),true);
      wc = ws.getWritableCell(0, 2);
      this.setBorderOK(ws,wf,jxl.format.Alignment.LEFT,wc.getContents(),true);
      wc =ws.getWritableCell(0,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.CENTRE,this.cellNumber,true);
      wc =ws.getWritableCell(0,8);
      this.setBorderOK(ws,wf,jxl.format.Alignment.LEFT,wc.getContents(),true);
      WritableFont underline = new WritableFont(WritableFont.ARIAL,
                                                WritableFont.DEFAULT_POINT_SIZE,
                                                WritableFont.NO_BOLD,
                                                false,
                                                UnderlineStyle.SINGLE);

      wc = ws.getWritableCell(1, 1);
      this.setBorderOK(ws,underline,jxl.format.Alignment.CENTRE,this.t1,false);
      wc =ws.getWritableCell(3,1);
      this.setBorderOK(ws,underline,jxl.format.Alignment.CENTRE,this.t2,false);
      wc =ws.getWritableCell(4,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.CENTRE,(this.waypointOfBegin+"--"+this.waypointOfEnd),true);
      wc =ws.getWritableCell(5,1);
      this.setBorderOK(ws,underline,jxl.format.Alignment.CENTRE,this.t3,false);
      wc =ws.getWritableCell(7,1);
      this.setBorderOK(ws,underline,jxl.format.Alignment.CENTRE,this.t4,false);
      wc =ws.getWritableCell(12,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.LEFT,this.t5,true);
      wc =ws.getWritableCell(14,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.LEFT,this.t6,true);
      wc =ws.getWritableCell(16,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.LEFT,this.t7,true);
      wc =ws.getWritableCell(2,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.CENTRE,tracksName,true);
      wc =ws.getWritableCell(4,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.CENTRE,wc.getContents(),true);
      wc =ws.getWritableCell(6,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.CENTRE,wc.getContents(),true);
      wc =ws.getWritableCell(7,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.CENTRE,this.disctance,true);
      wc =ws.getWritableCell(8,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.CENTRE,wc.getContents(),true);
      wc =ws.getWritableCell(9,5);
      this.setBorderOK(ws,wf,jxl.format.Alignment.CENTRE,this.cellType,true);
      wc =ws.getWritableCell(10,5);

⌨️ 快捷键说明

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