顯示具有 Apache套件_POI 標籤的文章。 顯示所有文章
顯示具有 Apache套件_POI 標籤的文章。 顯示所有文章

2012年6月12日 星期二

[JAVA_Apache]POI套件的使用_產出EXCEL文件_儲存格的樣式使用方法(CellStyle)

POI的使用
[JAVA_Apache]POI套件的使用_產出EXCEL文件_儲存格的樣式使用方法(CellStyle)


使用儲存格 設定為日期格式
 
public static void training5() throws IOException {
  Workbook wb = new HSSFWorkbook();
     //or Workbook wb = new XSSFWorkbook();
     CreationHelper createHelper = wb.getCreationHelper();
     Sheet sheet = wb.createSheet("new sheet");

     // Create a row
     Row row = sheet.createRow(0);
     // as a date.
     Cell cell = row.createCell(0);
     cell.setCellValue(new Date());

     CellStyle cellStyle = wb.createCellStyle();
     cellStyle.setDataFormat(
         createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
    
     cell = row.createCell(1);
     cell.setCellValue(new Date());
     cell.setCellStyle(cellStyle);

     //you can also set date as java.util.Calendar
     cell = row.createCell(2);
     cell.setCellValue(Calendar.getInstance());
     cell.setCellStyle(cellStyle);

     // Write the output to a file
     FileOutputStream fileOut = new FileOutputStream("workbook.xls");
     wb.write(fileOut);
     fileOut.close();
 }


 
public static void training6() throws IOException {
  Workbook wb = new HSSFWorkbook();
  // or Workbook wb = new XSSFWorkbook();
  CreationHelper createHelper = wb.getCreationHelper();
  Sheet sheet = wb.createSheet("new sheet");
  // Create a row
  Row row = sheet.createRow(0);
  Cell cell = row.createCell(0);
  // 第一種:日期格式
  
  cell.setCellValue(new Date());
  CellStyle cellStyle = wb.createCellStyle();
  DataFormat format = wb.createDataFormat();//
  cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));
  cell.setCellStyle(cellStyle);
  
  // 第二種:保留兩位小數格式
  cell = row.createCell(1);
  cell.setCellValue(1.2);
  CellStyle cellStyle2 = wb.createCellStyle();
  cellStyle2.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
  cell.setCellStyle(cellStyle2);
  // 第三種:貨幣格式
  cell = row.createCell(2);
  cell.setCellValue(20000);
  CellStyle cellStyle3 = wb.createCellStyle();
  DataFormat format3 = wb.createDataFormat();
  cellStyle3.setDataFormat(format3.getFormat("$#,##0"));
  cell.setCellStyle(cellStyle3);
  // 第四種:百分比格式
  cell = row.createCell(3);
  cell.setCellValue(20);
  CellStyle cellStyle4 = wb.createCellStyle();
  cellStyle4.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
  cell.setCellStyle(cellStyle4);
  // 第五种:中文大寫格式
  cell = row.createCell(4);
  cell.setCellValue(20000);
  CellStyle cellStyle5 = wb.createCellStyle();
  DataFormat format5 = wb.createDataFormat();
  cellStyle5.setDataFormat(format5.getFormat("[DbNum2][$-804]0"));
  cell.setCellStyle(cellStyle5);
  // 第六種:科學格式
  cell = row.createCell(5);
  cell.setCellValue(20000);
  CellStyle cellStyle6 = wb.createCellStyle();
  cellStyle6.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00E+00"));
  cell.setCellStyle(cellStyle6);
  // 第七種:自訂格式
  cell = row.createCell(6);
  cell.setCellValue(-20000);
  CellStyle cellStyle7 = wb.createCellStyle();
  DataFormat format7 = wb.createDataFormat();
  cellStyle7.setDataFormat(format7.getFormat("$#,##0.00_ ;[red](-$#,##0.00)"));
  cell.setCellStyle(cellStyle7);
  // Write the output to a file
  FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  wb.write(fileOut);
  fileOut.close();
 }
格式可找在excel裡的儲存格樣式->自訂
如圖










其它文章

[JAVA_Apache]POI套件的使用_產出EXCEL文件_儲存格的使用方法(Cell、row、column)

POI的使用
[JAVA_Apache]POI套件的使用_產出EXCEL文件_儲存格的使用方法(Cell、row、column)



 
public static void training4() throws IOException {
  Workbook wb = new HSSFWorkbook();
  //or Workbook wb = new XSSFWorkbook();
  Sheet sheet = wb.createSheet("new sheet");
  //一列(1、2、3…)  row ,從0開始
  //使用第一列
  Row row = sheet.createRow((short) 0);
  //創建一個儲存格
  //位置(row:column)
  //    A    B     C    D
  //1  0:0  0:1   0:2  0:3
  //2  1:0  1:1   1:2  1:3
  //3  2:0  2:1   2:2  2:3
  //使用第一列的第一個儲存格
  //方法一
  Cell cell = row.createCell(0);
  cell.setCellValue(1);
  //方法二
  row.createCell(1).setCellValue(1.5);
  //方法三
  CreationHelper createHelper = wb.getCreationHelper();
  row.createCell(2).setCellValue(createHelper.createRichTextString("test"));
  //方法四
  row.createCell(3).setCellValue(true);
  //方法五
  row.createCell(4).setCellValue("cell");
  //產出內容位置 
  //   A    B        C    D      E
  //1  1  1.5     test   TRUE   cell
  FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  wb.write(fileOut);
  fileOut.close();
 }











其它文章

[JAVA_Apache]POI套件的使用_產出EXCEL文件_創建EXCEL裡工作表的方法(Sheet)

POI的使用
[JAVA_Apache]POI套件的使用_產出EXCEL文件_創建EXCEL裡工作表的方法(Sheet)

產生新的工作表

 
public static void training3() throws IOException {
  FileOutputStream fileOut = null;
  try {
   // 這裡只純產生新的工作表
   // 產出Excel 97(XLS)文件格式
   Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
   Sheet sheet1 = wb.createSheet("new sheet");
   Sheet sheet2 = wb.createSheet("second sheet");
   //另外如果是取得原來就有的excel裡的工作表
   //方法一 :取得工作表名稱為:工作表1
   //wb.getSheet("工作表1");
   //方法二 :
   //此方法 是要看在excel中的工作表順序
   //hssfWorkbook.getSheetAt(0);
   fileOut = new FileOutputStream("workbook.xls");
   wb.write(fileOut);
   fileOut.close();
  }
  catch (IOException ioe) {
   ioe.printStackTrace();
  }
  finally {
   if (null != fileOut) {
    fileOut.close();
   }
  }
 }












其它文章

[JAVA_Apache]POI套件的使用_產出EXCEL文件_產出EXCEL方法(Workbook)

POI的使用
[JAVA_Apache]POI套件的使用_產出EXCEL文件_產出EXCEL方法(Workbook)

           
public static void training1() throws IOException {
 FileOutputStream fileOut = null ;
  try {
   //這裡只是純產出excel檔案而以
   //產出Excel 97(XLS)文件格式
   Workbook hssfwb = new HSSFWorkbook();
   fileOut = new FileOutputStream("workbook.xls");
   hssfwb.write(fileOut);
   fileOut.close();
   //Excel 2007的(XLSX)文件格式
   Workbook xssfwb = new XSSFWorkbook();
   fileOut = new FileOutputStream("workbook.xlsx");
   xssfwb.write(fileOut);
   fileOut.close();
  }
  catch (IOException ioe) {
   ioe.printStackTrace();
  }
  finally {
   if (null != fileOut) {
    fileOut.close();
   }
  }
 }


 
public static void training2() throws IOException {
  // 讀取一個原有的excel檔案
  FileInputStream fileInputStream = null;
  Workbook hssfWorkbook = null;
  try {
   fileInputStream = new FileInputStream(new File("D:/training1.xls"));
   if (null != fileInputStream) {
    try {
     hssfWorkbook = new HSSFWorkbook(fileInputStream);
    }
    catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  catch (IOException ioe) {
   ioe.printStackTrace();
   throw ioe;
  }
  finally {
   if (null != fileInputStream) {
    fileInputStream.close();
   }
  }
  //將剛讀取的excel檔案,產出為另一個excel檔案。
  FileOutputStream fileOutputStream = null;
  try {
   fileOutputStream = new FileOutputStream(new File("D:/trainingto.xls"));
   hssfWorkbook.write(fileOutputStream);
   fileOutputStream.flush();
  }
  catch (IOException ioe) {
   ioe.printStackTrace();
   throw ioe;
  }
  finally {
   if (null != fileOutputStream) {
    fileOutputStream.close();
   }
  }
 }











其它文章

2012年6月11日 星期一

[JAVA_Apache]POI套件的使用_產出EXCEL文件基本認識及JAR檔的取得

POI的使用
[JAVA_Apache]POI套件的使用_產出EXCEL文件基本認識及JAR檔的取得

一、取得JAR檔
        官網download
         Maven 參數

二、基本認識
       1、Microsoft Excel格式文件
             POI 不支援Excel 5.0/7.0 (BIFF5)

             目前POI有只支援BIFF8格式,POI分別名為HSSF(.xls)及XSSF(.xlsx).
              POI HSSF:Excel 97(XLS)文件格式
              POI XSSF:Excel 2007的(XLSX)文件格式。
              這裡只說明HSSF及XSSF,其它詳官網
        2、POI與EXCEL對應名稱
           
EXCEL
POI
一個EXCEL
Workbook
一個工作表1
Sheet
一列(123)
row
一欄(ABC)
column
儲存格格式
CellStyle



          3、POI官網開發指南












其它文章

標籤

Oracle (150) Oracle DB (144) Oracle_DB (143) Oracle SQL (135) JAVA (84) css-基本類 (65) MySQL (59) CSS Selector (58) jQuery (49) JavaScript-基本類 (39) Spring Boot (38) JavaScript (37) JavaScript HTML DOM (37) JavaScript-HTML_DOM (36) CSS3 (30) JAVA-基本類 (28) jQuery UI (27) Apache (23) Oracle GROUP BY (20) datepicker (20) Android (18) Oracle Date (17) c (17) JAVA-lang套件 (16) Linux (16) Oracle Sub Query (16) Spring-基本類 (16) jQuery-基本類 (16) MySQL-進階系列教學 (15) Android基本類 (14) Grails (14) Oracle join (14) SQLite (13) Spring (13) WIN7-基本類 (13) grails-基本類 (13) linux cent os (13) CKEditor (12) JAVA-流程控制類 (12) JAVA_Spring (12) PHP (11) Spring MVC (11) MySQL-基本系列教學 (10) Notepad (10) Notepad++ (10) SQLite for java (10) Windows (10) c/c++ (10) eclipse (9) jQuery-Selector (9) sqldeveloper (9) DB_Toad (8) JAVA_IDE_Eclipse (8) JavaScript-String類 (8) MySQL DB Toad (8) MySQL-DATE相關 (8) MySQL-函式相關 (8) Spring Bean (8) Android Studio (7) HTML5 (7) Hibernate (7) JAVA-OCWCD (7) JavaScript-陣列類 (7) Docker (6) JAVA-程式分享 (6) JAVA.util套件 (6) JavaScript-數學類 (6) MinGw (6) MySQL-其它類 (6) Servlet (6) centos (6) Apache_Tomcat (5) Apache套件_POI (5) CSS (5) JavaScript-Date物件 (5) JavaScript-其它類 (5) PostgreSQL (5) httpd (5) log4j (5) 基本資訊 (5) 開發工具 (5) CSS Properties (4) Dev-C++ (4) IntelliJ IDEA (4) Oracle DDL (4) Sublime (4) TortoiseSVN (4) apache_Maven (4) Android NDK (3) Eclipse IDE for C/C++ (3) Hibernate-基本類 (3) JAVA-問題 (3) JAVA-綀習分享 (3) JVM (3) Linux 指令 (3) Proxy Server (3) Spring Mobile (3) Spring web (3) Squid (3) VirtualBox (3) maven (3) zk (3) 生活其它 (3) Bootstrap (2) Filter (2) JAVA_IO (2) JAVA_其它_itext套件 (2) JBoss-問題 (2) JSP (2) Jboss (2) Listener (2) MySQL-語法快速查詢 (2) Spring AOP (2) Spring Batch (2) Spring Boot Actuator (2) Spring i18n (2) Subversive (2) Tomcat 8 (2) UML (2) WebJars (2) WinMerge (2) c++ (2) c語言綀習題 (2) jQuery Mobile (2) jQuery-事件處理 (2) jQuery-套件類 (2) putty (2) svn (2) weblogic (2) Apache_JMeter (1) Apache套件_BeanUtils (1) Apache套件_StringUtils (1) Base64 (1) Google API (1) HTML5-基本類 (1) Heap (1) JAVA 7 (1) JAVA SE 、JAVA EE、JAVA ME (1) JAVA 日期 (1) JAVA-OCJP (1) JAVA-WEB (1) JAVA_IDE (1) JAVA其它 (1) JBoss Server (1) JDK (1) JMX (1) JRE (1) Java RMI (1) Java String (1) Joda Time (1) Linux_其它 (1) MySQL教學 (1) Oracle_VirtualBox (1) SQL Server (1) SWT (1) Session (1) Stack (1) Struts 2 (1) Tool (1) ZK Studio (1) csv (1) grails-其它類 (1) jQuery-進階 (1) java mail (1) java web (1) java8 (1) jsoup (1) mockmvc (1) modules (1) tomcat (1) win10 (1) 其它類 (1) 圖片工具 (1) 模擬器 (1) 讀書分享 (1) 開發資訊 (1)

精選文章

初學 Java 的 HelloWorld 程式

撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...