歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java讀取以xls和csv為後綴名的EXCEL

1,Java讀取xls為後綴名的EXCEL

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.w3c.dom.*
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
/**
 * <p>Description: Java讀取xls為後綴名的EXCEL</p>
 * @author
 * @version 1.0
 */
public class UplpolicyNo {

 @SuppressWarnings("unchecked")
 public static void readExcelFile(String fileName) {
  // 獲取工作區
  Workbook workbook = null;
  try {
   workbook = Workbook.getWorkbook(new File(fileName));
  } catch (Exception e1) {
  }
  // 通過Workbook的getSheet方法選擇第一個工作簿(從0開始)
  Sheet sheet = workbook.getSheet(0);
  System.out.println("---------工作簿==" + sheet.getRows());
  String qunarPolicyno = "";
  for (int i = 0, n = sheet.getRows(); i < n; i++)
  {
   int j = 0;
   Cell c1 = sheet.getCell(j++, i);
   String policyno1 = c1.getContents();
  
   Cell c2 = sheet.getCell(j++, i);
   String policyno2 = c2.getContents();
  
   Cell c3 = sheet.getCell(j++, i);
   String policyno3 = c3.getContents();
  
   Cell c4 = sheet.getCell(j++, i);
   String policyno4 = c4.getContents();
  
   Cell c5 = sheet.getCell(j++, i);
   String policyno5 = c5.getContents();
  
   if (policyno1.equals("") && policyno2.equals("") &&
    policyno3.equals("") && policyno4.equals("") && policyno5.equals("")) {
    String errorInfo = "上傳(Excel)第" + (i - 9) + "行終止!";
    System.out.println(errorInfo);
    break;
   }
  }
  workbook.close();
 }

 

 

2,Java讀取以csv為後綴名的EXCEL

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import jxl.Sheet;
import jxl.Workbook;
/**
 * <p>Description: Java讀取以csv為後綴名的EXCEL</p>
 * @author
 * @version 1.0
 */
public class QunarHaihangBillCompareUpUtil {
 
 
  public static void  CSVAnalysis(String fileName,HttpServletRequest request)
  { 
        InputStreamReader fr = null; 
        BufferedReader br = null; 
        int k = 0;
        try { 
            fr = new InputStreamReader(new FileInputStream(fileName)); 
            br = new BufferedReader(fr); 
            String rec = null; 
            String[] argsArr = null; 
            String orderNum = "";
            String period = "";
            while ((rec = br.readLine()) != null)
            { 
             ++k;
             if(k != 1)
             {
                 argsArr = rec.split(","); 
                 orderNum = argsArr[0];
                 period = argsArr[1];
             }
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                if (fr != null) 
                    fr.close(); 
                if (br != null) 
                    br.close(); 
            } catch (IOException ex) { 
                ex.printStackTrace(); 
            } 
        }
    } 

Copyright © Linux教程網 All Rights Reserved