Total Pageviews

2016/09/30

[Travel] 台東

多良車站

鹿野高台


台東太麻里海岸線


台東金針山




台東縣小野柳風景區


台東藝術鐵花村



太麻里 第一道曙光紀念公園


利吉惡地


2016/09/09

[Java] try-with-resources statement

JDK 7 introduces a new version of try statement known as try-with-resources statement. This feature add another way to exception handling with resources management,it is also referred to as automatic resource management.

If this class implements AutoClossable interface, then you can make good use of  try-with-resources statement. Do not need to close manually.
e

As-is (does not apply try-with-resources statement)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  FileOutputStream outputStream = null;
  File tmpFile = null;
  try {
      tmpFile = File.createTempFile("issue", ".xls");
      outputStream = new FileOutputStream(tmpFile);
      workbook.write(outputStream);
  } catch (FileNotFoundException e) {
      log.error(e.getMessage(), e);
  } finally {
      if (outputStream != null) {
          outputStream.close();
      }
      if (workbook != null) {
          workbook.close();
      }
  }


To-be (apply try-with-resources statement)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  File tmpFile = File.createTempFile("issue", ".xls");
  try(FileOutputStream outputStream  = new FileOutputStream(tmpFile)){
      workbook.write(outputStream);
  } catch (FileNotFoundException e) {
      log.error(e.getMessage(), e);
  } finally{
      if (workbook != null) {
          workbook.close();
      }
  }


Reference
[1] http://www.studytonight.com/java/try-with-resource-statement.php

2016/09/08

[Apache POI] How to freeze the first row

Problem
I am using Apache POI to write data into Microsoft Excel. 
If I would like to keep the first row on the top of this document when I scroll down, how to do it?


How-to
You can utilize createFreezePane API which provide by Apache POI to fulfill this requirement. Here has code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 @Override
 public File exportIssuesToXls(List < Issue > issues) throws IOException, RedmineException {
  Workbook workbook = new HSSFWorkbook();
  Sheet sheet = workbook.createSheet("issue list");
  CellStyle style = createCellStyle(workbook);

  //....

  //freeze the first row
  sheet.createFreezePane(0, 1);

  FileOutputStream outputStream = null;
  File tmpFile = null;
  try {
   tmpFile = File.createTempFile("issue", ".xls");
   log.info("tmpFile = " + tmpFile.getCanonicalPath());
   outputStream = new FileOutputStream(tmpFile);
   workbook.write(outputStream);

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } finally {
   if (outputStream != null) {
    outputStream.close();
   }
   if (workbook != null) {
    workbook.close();
   }
  }
  return tmpFile;
 }



Reference
[1] https://stackoverflow.com/questions/17932575/apache-poi-locking-header-rows

2016/09/07

[Apache POI] How to add new line in cell data

Problem
I am using Apache POI to write data into Microsoft Excel.
I would like to break data into new line in some cell, how to do it?


How-to
You can add \n for the new line. For example,
1
Issue issue1 = new Issue(1, "查不到資料", "新建立", "正常", "蜘蛛人(2016-05-26 17:05:00):\n這個提議不錯,來做吧!\n\n浩克(2016-05-26 17:05:00):\n測試無誤\n\n");




Reference
[1] https://stackoverflow.com/questions/14646349/how-to-add-new-line-character-in-the-cell-comment-using-apache-poi

2016/09/06

[Microsoft Word] 如何新增檔名到頁尾

Problem
如果我要在 word 文件的頁尾插入 word 檔名,在 Microsoft Word 中如何做?

How-to
1. 點選頁尾


2. 插入 => 快速組件 => 功能變數


3. 選擇 file name,按下確定按鈕


4. 此時,就可以看到頁尾出現此檔案的檔名


Reference
[1] https://support.office.com/zh-tw/article/%E6%96%B0%E5%A2%9E%E6%AA%94%E5%90%8D%E5%88%B0%E9%A0%81%E9%A6%96%E6%88%96%E9%A0%81%E5%B0%BE-dc62245f-b6af-45b2-a521-17753fc3539e