Total Pageviews

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 

2016/09/05

[Windows 7] 參考到的帳戶目前已鎖定,且可能無法登入

Problem
有次在登入windows 7的帳號密碼時,因故輸入三次錯誤,導致電腦被鎖定,即便是重新開機以後還是出現相同的錯誤訊息


Solution
由於之前已經有過一次經驗,必須要有windows 7光碟進入才有辦法解決,故經過上次以後,有建立一個擁有 administration 權限的另外一個 user

所以在本次,我只要先重開機,按下F8,選擇以安全模式進入Windows,再利用另外一個帳號log in 進去

Steps:
1. 在我的電腦按下右鍵,選擇管理


2 .系統工具=>本機使用者與群組=>使用者


3. 於 lock 的使用者帳號按下滑鼠右鍵,選擇內容



4. 將最後一個 checkbox 勾勾取消,並重新開機即可