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(); 
            } 
        }
    } 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/9832e53b1ec9544a8e4fb8bc8a3f4337.html