javaexcel數據導入
❶ 用java實現excel與資料庫之間的導入導出
public static List<String> excels(int rowStart,int cellStart,String filepath) throws Exception {
//存放從Excel中讀取到的內容
List<String> result = new ArrayList<String>();
//創建對Excel工作薄文件的引用
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(filepath));
//獲取表格的所有sheet
int num=workbook.getNumberOfSheets();
for(int n=0;n<num;n++){
//創建對工作表的引用
XSSFSheet sheet = workbook.getSheetAt(n);
//Excel的行元素
XSSFRow row;
//遍歷行
for(int i=rowStart-1;i<sheet.getPhysicalNumberOfRows();i++){
String bean="";
row = sheet.getRow(i);
//如果整行為空,結束此次循環執行下一次循環
if(row == null){
continue;
}
//遍歷列
for(int j=cellStart-1;j<row.getPhysicalNumberOfCells();j++){
Cell content = row.getCell(j);
//如果但換個數據為null,結束此次循環執行下一次(可以為"")
if(content== null) {
continue;
}
content.setCellType(Cell.CELL_TYPE_STRING);
String value = content.getStringCellValue();
if(j==(row.getPhysicalNumberOfCells())){
bean+=value;
}
bean+=value+",";
}
result.add(bean);
}
}
return result;
}
❷ java實現excel導入數據怎麼讀取
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class Test0 {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException,
IOException {
File file = new File("C://test01.xls");// Excel文件路徑
String[][] result = getData(file, 1);
int rowLength = result.length;
for (int i = 0; i < rowLength; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.print(result[i][j] + "\t\t");
}
System.out.println();
}
}
/**
* 讀取Excel的內容,第一維數組存儲的是一行中格列的值,二維數組存儲的是多少個行
*
* @param file
* 讀取數據的源Excel
* @param ignoreRows
* 讀取數據忽略的行數,比喻行頭不需要讀入 忽略的行數為1
* @return 讀出的Excel中數據的內容
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
List<String[]> result = new ArrayList<String[]>();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
// 打開HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行為標題,不取
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
// 注意:一定要設成這個,否則可能會出現亂碼
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 導入時如果為公式生成的數據則無值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}
}
if (columnIndex == 0 && value.trim().equals("")) {
break;
}
values[columnIndex] = rightTrim(value);
hasValue = true;
}
if (hasValue) {
result.add(values);
}
}
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
return returnArray;
}
/**
* 去掉字元串右邊的空格
*
* @param str
* 要處理的字元串
* @return 處理後的字元串
*/
public static String rightTrim(String str) {
if (str == null) {
return "";
}
int length = str.length();
for (int i = length - 1; i >= 0; i--) {
if (str.charAt(i) != 0x20) {
break;
}
length--;
}
return str.substring(0, length);
}
}
❸ 用JAVA,Excel里的數據如何能導入資料庫
估計沒人會給你講,這個要用控制項,或者是直接通過資料庫命令將excel往庫裡面導
❹ 如何把excel數據導入java
用第三方組件poi讀取,給個例子你參考一下
public class ApachePOIExcelRead {
private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";
public static void main(String[] args) {
try {
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
//getCellTypeEnum shown as deprecated for version 3.15
//getCellTypeEnum ill be renamed to getCellType starting from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
System.out.print(currentCell.getStringCellValue() + "--");
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print(currentCell.getNumericCellValue() + "--");
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
❺ 如何在Java中導入Excel表數據
使用jxl這個包導入這個包下的importjxl.Workbook;importjxl.write.Label;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;然後Filef=newFile("d:/view.xls");//獲得文件WritableWorkbookwb=Workbook.createWorkbook(f);//可以讀寫的workbookWritableSheets=wb.createSheet("第一頁",0);//workbook中的sheet,就是專在excel下面那個sheet1,sheet2.這個方法屬表名使用第一個sheet並且命名為"第一頁"Labellable=null;//label就是某一個小單元格2層循環,給每個labellabel=newLabel(列,行,值);//都是從0開始的.就是給第幾列地幾行的labe里寫東西s.addCell(l);//把這個lable加入到sheet中最後wb.write();wb.close();讀的也差不多.你網路一下jxl,到處都是例子的哇望點贊
❻ 怎樣利用java實現把數據自動導入excel
給你個例子,轉自xiaosa1984。自己按照自己的要求修改即可。jxl.jar 這個得下載導入。
需要導入jxl.jar
搭建環境
將下載後的文件解包,得到jxl.jar,放入classpath,安裝就完成了。
創建文件
擬生成一個名為「測試數據.xls」的Excel文件,其中第一個工作表被命名為「第一頁」,大致效果如下:
代碼(CreateXLS.java):
//生成Excel的類
import java.io.*;
import jxl.*;
import jxl.write.*;
public class CreateXLS
{
public static void main(String args[])
{
try
{
//打開文件
WritableWorkbook book=
Workbook.createWorkbook(new File(「測試.xls」));
//生成名為「第一頁」的工作表,參數0表示這是第一頁
WritableSheet sheet=book.createSheet(「第一頁」,0);
//在Label對象的構造子中指名單元格位置是第一列第一行(0,0)
//以及單元格內容為test
Label label=new Label(0,0,」test」);
//將定義好的單元格添加到工作表中
sheet.addCell(label);
/*生成一個保存數字的單元格
必須使用Number的完整包路徑,否則有語法歧義
單元格位置是第二列,第一行,值為789.123*/
jxl.write.Number number = new jxl.write.Number(1,0,789.123);
sheet.addCell(number);
//寫入數據並關閉文件
book.write();
book.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
編譯執行後,會在當前位置產生一個Excel文件。
更加詳細內容請見http://..com/question/118637923.html