坤弟的 POI 解析Excel 包括(double 转 字符串 问题)

package com.primeton.yc.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.w3c.dom.*;

import com.primeton.tp.common.xml.XmlUtil;
import com.primeton.tp.core.api.BizContext;
/**
* @author admin
* @version 1.0
* @date 2009-6-22
* @class_displayName ParsExcel
*/

public class ParsExcel {

public static void main(String[] args) throws Exception {
System.out.println("========start=========");
parshExcel();
System.out.println("========end=========");
}

/**
*
* @param doc type: Document, DOM;
* @param param type: BizContext;
* @return: int ,运算逻辑返回值,如果失败返回0,成功返回1
* @throws Exception
* <p>
* ** bizlet 的显示名称 **
* @bizlet_displayName parshExcel
*/
//public static int parshExcel(Document doc, BizContext param) throws Exception{
public static int parshExcel() throws Exception{
String filePath = "f:/aa.xls";//(String) param.getParaObjectAt(0);
System.out.println("========="+filePath);
String tableName = "CUST";//(String)param.getParaObjectAt(1);
//Node param1 = (Node) param.getParaObjectAt(2);
List sqls = new ArrayList();
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook(new FileInputStream(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int len = workbook.getNumberOfSheets();
for (int i = 0; i < len; i++) {
// 获得Excel的Sheet
HSSFSheet sheet = workbook.getSheetAt(i);
int sNum = 0;
int rows = sheet.getPhysicalNumberOfRows();

StringBuffer sql = new StringBuffer("").append("insert into "+tableName+"(");
//取第一行的每列作为字段名,必须和数据库字段名相对应
HSSFRow titleRow = sheet.getRow(0);
if(titleRow!=null){
int titleCells = titleRow.getPhysicalNumberOfCells();
HSSFCell titleCell = null;
for(int c=0;c<titleCells;c++){
titleCell = titleRow.getCell((short)c);
String titleCellValue=titleCell.getStringCellValue();
System.out.println("titleCellValue="+titleCellValue);
sql = sql.append(titleCellValue+",");
}
        String sql1 = sql.substring(0, sql.length()-1);
sql = new StringBuffer(sql1).append(") values(");
}

String sqlbak = sql.toString();
// 按行读取excel中的数据

for (int r = 1; r < rows; r++){
// 获得Sheet中的一行
HSSFRow row = sheet.getRow(r);
HSSFCell cell = null;
if (row != null) {
int cells = row.getPhysicalNumberOfCells();

for(int c=0;c<cells;c++){
System.out.println("cells.length="+cells);
cell = row.getCell((short)c);
if(cell!=null) {
int cellType=cell.getCellType();
if(cellType==HSSFCell.CELL_TYPE_STRING){
sql = sql.append(cell.getStringCellValue()+",");
}else if(cellType==HSSFCell.CELL_TYPE_NUMERIC){
java.text.DecimalFormat formatter = new java.text.DecimalFormat("########");
String str = formatter.format(cell.getNumericCellValue());
sql = sql.append(str+",");
}
}else{
sql = sql.append("null,");
}
}

String esql = sql.substring(0, sql.length()-1)+");";
sqls.add(esql);
sql = new StringBuffer(sqlbak);
}
}
}

StringBuffer sqlList = new StringBuffer();
//sqlList.append("<list>");
for(int i=0;i<sqls.size();i++){
sqlList.append("<myEntity><sql>").append(sqls.get(i)).append("</sql></myEntity>");
}
//sqlList.append("</list>");
//XmlUtil.setNodeValue( param1, sqlList.toString() );
System.out.println(sqlList);
return 1;
}

}

/*
<list>
<myEntity name="sql1">
<sql>sqls[0]</sql>
</myEntity>
<myEntity name="sql2">
<sql>sqls[1]</sql>
</myEntity>
<list>
*/

你可能感兴趣的:(java,apache,sql,c,Excel)