読み込んだデータを使用する
次の例では、筆者の番組録画コレクションの情報をコンソールに出力しています。
public static void main(String args[])
{
ArrayList<String> colNames = null;
ArrayList<Map<String, Object>> columnMaps = null;
ExcelReader excelReader = null;
Iterator<String> colNamesIt = null;
Iterator<Map<String, Object>> columnMapsIt = null;
Map<String, Object> columnMap = null;
String colKey = null;
String[] myRecordings = new
String[]{"NCIS", "DoctorWho"};
try
{
excelReader = getInstance("demo_data.xls");
for(int i=0; i < myRecordings.length; i++)
{
colNames = excelReader.getColNames(myRecordings[i]);
colNamesIt = colNames.iterator();
columnMaps = excelReader.getMappedValues(myRecordings[i]);
columnMapsIt = columnMaps.iterator();
while(columnMapsIt.hasNext())
{
columnMap = columnMapsIt.next();
while(colNamesIt.hasNext())
{
colKey = colNamesIt.next();
System.out.println(colKey+"\t"+
((columnMap.get(colKey)!=null)?
columnMap.get(colKey):""));
}
System.out.println("---------------------------------");
colNamesIt = colNames.iterator();
}
}
}
catch (Exception any)
{
any.printStackTrace();
}
}
わずかな変更で、これをHTMLテーブルとして表示することもできます。
<tr><% while(colNamesIt.hasNext()){%>
<th><%=colNamesIt.next() %></th>
<%}colNamesIt = colNames.iterator();%></tr>
<% while(columnMapsIt.hasNext()){ %><tr>
<% columnMap = columnMapsIt.next(); while(colNamesIt.hasNext()){
colKey = colNamesIt.next();
isNeed = colKey.equals("Status")
&& columnMap.get(colKey)==null;%>
<td<%if(isNeed){ %> bgcolor="red"<%} %>>
<%=((columnMap.get(colKey)!=null)?columnMap.get(colKey):"Need") %>
</td>
<%}colNamesIt = colNames.iterator();%></tr>
<%} %></table></td><%}%></tr></table><%}catch
(Exception any){any.printStackTrace();}%>
フィールドの名前と値を取り出し、SQLのinsert文やupdate文を作成してもよいでしょう。
まとめ
非技術系ユーザーがMicrosoft Excelファイル形式のデータを自分で送信できるようにすることは、開発者にとってもユーザーにとっても利益をもたらします。POIプロジェクトのHSSFおよびXSSF APIを利用すると、これをJava EEの任意のWebプロジェクトで実現できます。
参考までに、HSSFは「Horrible Spread Sheet Format」の略で、XSSFは「XML Spread Sheet Format」の略です。前者はExcel 2007以前に対応し、後者はExcel 2007以降に対応します。大多数のユーザーはまだ以前のExcelフォーマットを使用しているので、本稿ではHSSFの例だけを掲載しました。MS Officeアップグレードの無料コピーがあれば新しいAPIもテストしたのですが、残念ながらそういうものは手に入らないようですね。

