วันอาทิตย์ที่ 11 กรกฎาคม พ.ศ. 2553

Application import file(Excel)

JExcelApi เป็น java library ที่เอางานเกี่ยวกับ M$ Excel โดยสามารถที่จะ อ่าน, เขียน และทำการแก้ไข M$ Excel ได้
ก่อนอื่นนั้นต้องนำ library ของ jxl มาใส่ใน Folder : lib ก่อน

1. สร้างหน้าจอเพื่อต้องการเรียกไฟล์ excel มาแสดงผล






2. สร้าง controller.xml โดยไม่ต้องใช้ view-map เพื่อเรียกใช้ processImportFile ที่ไฟล์ AExcelEvents.java










3. สร้างไฟล์แบบ java โดยเมธอดชื่อ processImportFile

package com.orangegears.util;

import java.io.IOException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;

import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.service.LocalDispatcher;


public class AExcelEvents {
public static final String module = AExcelEvents.class.getName();

public static String processImportFile(HttpServletRequest request, HttpServletResponse response) {
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
Debug.logInfo("1==============================================", module);

try
{
ServletInputStream is = null;
try {
is = request.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] junk = new byte[1024];
int bytesRead = 0;

// the first four lines are request junk
try {
bytesRead = is.readLine(junk, 0, junk.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bytesRead = is.readLine(junk, 0, junk.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bytesRead = is.readLine(junk, 0, junk.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bytesRead = is.readLine(junk, 0, junk.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
Workbook workbook = Workbook.getWorkbook(is);
Sheet sheet = (Sheet) workbook.getSheet("Sheet1");

Debug.logInfo("=============================================="+sheet.getCell(0, 0).getContents(), module);
Debug.logInfo("=============================================="+sheet.getCell(0, 1).getContents(), module);
Debug.logInfo("=============================================="+sheet.getCell(1, 0).getContents(), module);
Debug.logInfo("=============================================="+sheet.getCell(1, 1).getContents(), module);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
catch (JXLException e)
{

}
Debug.logInfo("2==============================================", module);
return "success";
}
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น