将Excel数据转换为MySql表格 [英] Conversion of excel data to MySql tables
本文介绍了将Excel数据转换为MySql表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将Excel文件数据转换为MySql表格。
我已经提到以下链接。
http://www.coderanch.com/t/608700/JDBC/databases/import-data-excel-files-database
任何人都可以为我发送一个有用的链接或简单的java代码吗?
解决方案
这是您如何读取Excel文件并将其存储在集合对象中
import java.io. *;
import java.util.Iterator;
导入org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
公共类ReadExcelFile {
FileInputStream file = new FileInputStream {
public static void main(String [] args)
{
try {
(新文件(C:/Users/hussain.a/Desktop/newExcelFile.xlsx));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator< Row> rowIterator = sheet.iterator();
rowIterator.next(); (rowIterator.hasNext())
{
Row row = rowIterator.next();
//对于每一行,遍历每一列
Iterator< Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(boolean ===>>>+ cell .getBooleanCellValue()+\t);
//在此处写入hibernate行以将其存储在您的域中
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(numeric ===>>>+ cell.getNumericCellValue()+\t);
//在此处写入hibernate行以将其存储在您的域中
break;
case Cell.CELL_TYPE_STRING:
System.out.println(String ===>>>+ cell.getStringCellValue()+\t);
//在此处写入hibernate行以将其存储在您的域中
break;
}
}
System.out.println();
}
file.close();
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
}
之后您可以使用hibernate并将其存储在您的域类中。
I am developing an application in spring and hibernate with Eclipse as IDE.
I want to convert Excel file data to MySql table.
I have referred following link.
http://www.coderanch.com/t/608700/JDBC/databases/import-data-excel-files-database
Can anybody send me an useful link or simple java code for the same?
解决方案
this is how you read Excel file and store in collection object
import java.io.*;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelFile {
public static void main(String[] args)
{
try {
FileInputStream file = new FileInputStream(new File("C:/Users/hussain.a/Desktop/newExcelFile.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
System.out.println("boolean===>>>"+cell.getBooleanCellValue() + "\t");
// write hibernate lines here to store it in your domain
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println("numeric===>>>"+cell.getNumericCellValue() + "\t");
// write hibernate lines here to store it in your domain
break;
case Cell.CELL_TYPE_STRING:
System.out.println("String===>>>"+cell.getStringCellValue() + "\t");
// write hibernate lines here to store it in your domain
break;
}
}
System.out.println("");
}
file.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
after this you can use hibernate and store in your domain classes
这篇关于将Excel数据转换为MySql表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文