POI 해석 EXCEL, EXCEL 내보내기
package com.renesola.datafeed.util;
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.renesola.framework.util.DateUtil;
import com.renesola.framework.util.StringUtil;
import com.renesola.shop.util.ShopUtil;
import com.renesola.framework.util.StringUtil;
public class ExcelUtil {
    private static ExcelUtil instance = new ExcelUtil();
    public static ExcelUtil getInstance() {
        return instance;
    }
    protected ExcelUtil() {
    }
    public int getColumnCount(HSSFSheet sheet, int maxRow) {
        int col = 0;
        try {
            if (sheet != null) {
                int rowCount = sheet.getLastRowNum();
                if (rowCount > maxRow) {
                    rowCount = maxRow;
                }
                for (int i = 0; i <= rowCount; i++) {
                    HSSFRow row = sheet.getRow(i);
                    if (row == null) {
                        continue;
                    }
                    if (row.getPhysicalNumberOfCells() > col) {
                        col = row.getPhysicalNumberOfCells();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return col;
    }
    public List doRead(String filepath) throws Exception {
        List list = new ArrayList();
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filepath));
        HSSFSheet sheet = workbook.getSheetAt(0);
        int colmunCount = getColumnCount(sheet, 4);
        DecimalFormat df = new DecimalFormat("0.##########");
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            int cols=row.getFirstCellNum() + colmunCount;
            if (row.getPhysicalNumberOfCells() > cols) {
                cols=row.getPhysicalNumberOfCells();
            }
            if(row.getLastCellNum()<1){
                continue;
            }
            String[] s = new String[cols];
            for (int j = row.getFirstCellNum(); j < cols; j++) {
                HSSFCell cell = row.getCell((short) j);
                if (cell == null) {
                    s[j] = "";
                } else {
                    if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                        s[j] = df.format(cell.getNumericCellValue());
                    } else {
                        s[j] = cell.toString();
                    }
                }
            }
            list.add(s);
        }
        return list;
    }
    public String trimSpace(String txt) {
        if (!StringUtil.isEmpty(txt) || "null".equalsIgnoreCase(txt)) {
            StringBuffer sb = new StringBuffer();
            char t1 = '
', t2 = '\r', t3 = '\t', t4 = ' ', t5 = ' ';
            for (int i = 0; i < txt.length(); i++) {
                char c = txt.charAt(i);
                if (c == t1 || c == t2 || c == t3 || c == t4) {
                    sb.append(t5);
                } else {
                    sb.append(c);
                }
            }
            txt = StringUtil.replace(sb.toString(), "  ", " ");
            return txt;
        }
        return "";
    }
    public boolean isEmpty(String[] s) {
        if (s == null || s.length < 1) { return true; }
        int row = 0;
        for (int i = 0; i < s.length; i++) {
            if (StringUtil.isEmpty(s[i])) {
                ++row;
            }
        }
        return row == s.length;
    }
    public boolean exportExcel(List list, String exportFileName) throws Exception {
        if (list == null || list.size() <= 2) {
            return false;
        } else {
            String exportPath = exportFileName;
            File file = new File(exportPath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            WritableWorkbook workbook = Workbook.createWorkbook(new File(exportPath));
            WritableSheet sheet = workbook.createSheet("Sheet1", 0);
            for (int i = 0; i < list.size(); i++) {
                String[] contents = list.get(i);
                for (int j = 0; j < contents.length; j++) {
                    String content = contents[j];
                    if (!StringUtil.isEmpty(content)) {
                        Label label = new Label(j, i, contents[j]);
                        sheet.addCell(label);
                    }
                }
            }
            workbook.write();
            workbook.close();
            return true;
        }
    }
    public static void main(String[] args) {
        ExcelUtil obj = new ExcelUtil();
        try {
            List list = obj.doRead("d:/Country.xls");
            StringUtil.print(list);
            list = new ArrayList();
            list.add(new String[] { "Region.data_sync", "data_sync" });
            list.add(new String[] { "REGION_NAME" });
            list.add(new String[] { "Africa" });
            list.add(new String[] { "Asia Pacific" });
            list.add(new String[] { "Australia" });
            list.add(new String[] { "EU" });
            list.add(new String[] { "Middle East" });
            list.add(new String[] { "North America" });
            obj.exportExcel(list, "d:/Region_test.xls");
            list = obj.doRead("d:/Region_test.xls");
            StringUtil.print(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
      이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.