- 博客(0)
- 资源 (3)
- 收藏
- 关注
java解析excel到mysql代码存档.txt
package importEx;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ImportExcelTest {
/**
*
* 只能插入单选题和多选题
*
* @param args
* @throws BiffException
* @throws IOException
*/
public static void main(String[] args) throws BiffException, IOException {
//文件路径
InputStream is=new FileInputStream("d:\\c.xls");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/exam", "root", "aaaaaa");
PreparedStatement prep = conn.prepareStatement("insert into t_exam_question values (null,null,null,?,null,?,?,?,?,?,?,?,?,?,?,null,null)");
Workbook wb = Workbook.getWorkbook(is);
Sheet sheet = wb.getSheet(0); //第几张表格 从零开始
String sheetName = sheet.getName();
System.out.println("sheet表名(工作薄名): "+sheetName);
String question_content = ""; //题目内容
String question_type= ""; //题目类型
//用来得到单元格的内容
String choice_A = ""; //A
String choice_B=""; //B
String choice_C=""; //C
String choice_D=""; //D
String choice_E=""; //E
String choice_F=""; //F
String difficulty =""; //难度
String answer=""; //答案
String knowledge_type=""; //知识结构
if(sheet!=null)
{
// 获取表格总列数
int rsColumns = sheet.getColumns();
System.out.println("表格总列数rsColumns = " + rsColumns);
// 获取表格总行数
int rsRows = sheet.getRows();
System.out.println("表格总行数 rsRows = " + rsRows);
for(int rowNum = 1;rowNum <rsRows;rowNum++){
Cell[] cells=sheet.getRow(rowNum);
//这里就是对每一个列来获取,cells就是把这一行的某一列赋值给你所要操作的值.
question_content = cells[0].getContents();
difficulty = cells[8].getContents();
knowledge_type = cells[9].getContents();
choice_A = cells[1].getContents();
choice_B = cells[2].getContents();
choice_C = cells[3].getContents();
choice_D = cells[4].getContents();
choice_E = cells[5].getContents();
choice_F = cells[6].getContents();
answer = cells[7].getContents();
prep.setString(1, question_content);
prep.setInt(2, Integer.parseInt(sheetName));
prep.setInt(3, Integer.parseInt(difficulty));
prep.setInt(4, Integer.parseInt(knowledge_type));
prep.setString(5, choice_A);
prep.setString(6, choice_B);
prep.setString(7, choice_C);
prep.setString(8, choice_D);
prep.setString(9, choice_E);
prep.setString(10, choice_F);
prep.setString(11, answer);
prep.executeUpdate();
System.out.println("------------------第 "+rowNum+" 条记录");
}
}
prep.close();
conn.close();
wb.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据插入完成!!!程序终止!!!");
}
}
}
2010-10-21
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人