1.java通过poi把excel文件导入mysql数据库报错
2.Your stream was neither an OLE2 stream,源码 nor an OOXML stream
3.HSSF XSSF SXSSF
4.å¦ä½ç¨Apache POI读åExcel
java通过poi把excel文件导入mysql数据库报错
java通过poi把excel文件导入mysql数据库报错是因为excel中的数据类型要跟mysql中的数据类型和长度对应,否则类型转换异常是源码gba游戏源码破解最常见的。所以插入到mysql数据库的源码android源码 音频播放时候需要做类型检查。1、源码http post android 源码Excel中的源码码农周刊源码测试数据:
2、数据库表结构:
CREATE TABLE `student_info` (
`id` int() NOT NULL AUTO_INCREMENT,源码最新业务网站源码
`no` varchar() DEFAULT NULL,
`name` varchar() DEFAULT NULL,
`age` varchar() DEFAULT NULL,
`score` float DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3、java源码部分ReadExcel.java:
/
*** 读取excel中的源码数据并插入db
*/
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.b.common.Common;
import com.b.excel.vo.Student;
/
*** @author pieryon
* @created -5-
*/
public class ReadExcel {
public List<Student> readXls() throws IOException {
InputStream is = new FileInputStream(Common.EXCEL_PATH);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
student = new Student();
HSSFCell no = hssfRow.getCell(0);
HSSFCell name = hssfRow.getCell(1);
HSSFCell age = hssfRow.getCell(2);
HSSFCell score = hssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
return list;
}
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}
}
2、SaveData2DB.java
/
*** 插入数据到db
*/
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import com.b.common.Common;
import com.b.excel.util.DbUtil;
import com.b.excel.vo.Student;
/
*** @author pieryon
* @created -5-
*/
public class SaveData2DB {
@SuppressWarnings({ "rawtypes" })
public void save() throws IOException,源码 SQLException {
ReadExcel xlsMain = new ReadExcel();
Student student = null;
List<Student> list = xlsMain.readXls();
for (int i = 0; i < list.size(); i++) {
student = list.get(i);
List l = DbUtil.selectOne(Common.SELECT_STUDENT_SQL + "'%" + student.getName() + "%'", student);
if (!l.contains(1)) {
DbUtil.insert(Common.INSERT_STUDENT_SQL, student);
} else {
System.out.println("The Record was Exist : No. = " + student.getNo() + " , Name = " + student.getName() + ", Age = " + student.getAge() + ", and has been throw away!");
}
}
}
}
保存结果:
Your stream was neither an OLE2 stream, nor an OOXML stream
æè¿ä½¿ç¨Apache poi åexcel导åºçåè½ï¼éå°äºå¦ä¸é®é¢ï¼èµ·å对æ¯å ¶ä»çwebå·¥ç¨ï¼æ²¡æåç°å¦ä½è§£å³ãæåæ¾å°æåºæ¤å¼å¸¸çæºç ï¼
org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:)
å¯ä»¥çå°ï¼å¨è°ç¨ WorkbookFactory.create(inputStream) æ¥å建Workbookæ¶ï¼ä¼æ ¹æ®æ件类å(æ件头)æ¥åºå«ãå建åéçWorkbook对象ãå¦æä¸æ»¡è¶³å两个ifæ¡ä»¶éç hasPOIFSHeader() æ¹æ³ï¼å°±ä¼æåºå¼å¸¸ãè两个iféçhasPOIFSHeader()æ¹æ³å°±æ¯é对ExcelåExcelçå¤æï¼è¯»åæ件æµä¸çæ件头(byte[8])ä¿¡æ¯å»å¤æã
é£æ¢ç¶æ¯æ ¹æ®æ件ç头é¨ä¿¡æ¯å»æ¯å¯¹è¿è¡å¤æçï¼å¹¶ä¸excelåªæå两个çæ¬ï¼é£ä¸ºä»ä¹æ¾å¨classpathä¸çexcel模æ¿è¯»ååå建WorkBookæ¶ï¼æ件headerå¤æé½ä¸ç¬¦åå¢ï¼
æåæ¾å°åå ï¼mavenç¼è¯æå æ¶ï¼å°resourcesä¸çèµæºæ件转ç äºãæç»webå·¥ç¨æåºçjar/warå ï¼éé¢å½æ¡£è¿å»çexcel模æ¿æ件é½æ¯ä¹±ç ï¼æ件头信æ¯è¢«ä¿®æ¹ï¼å¯¼è´poiæ ¹æ¬æ æ³è¯å«è¿æ ·çexcelæ件ã
项ç®çpom.xmlä¸æ·»å mavenèµæºæ件
1ãæ¨è使ç¨poi-ooxmlä¸ç WorkbookFactory.create(inputStream) æ¥å建Workbookï¼å 为HSSFWorkbookåXSSFWorkbooké½å®ç°äºWorkbookæ¥å£ã(ExcelåExcel两个çæ¬å¨æ¤éè¿æ件headerè¿è¡éé )
2ãå½ä½ çå·¥ç¨ä¸ï¼éè¦æ¾å ¥ä¸äºéæèµæºæ件ä½ä¸ºæ¨¡æ¿ï¼æ¯å¦excelå¡«å 模æ¿ï¼word模æ¿(éé¢æäºåºå®æ ·å¼ï¼ç¨åºè¿è¡æ¶ç¨æ¨¡æ¿å¯¼åºæ¥è¡¨ç±»ç)ï¼è¿ä¸ªæ¶åï¼æ好é½é ç½®mavenå»é¤èµæºæ件ä¸è¢«è½¬ç ãä¸é¢çé¾æ¥æ2ç§æ¹å¼ã
Maven æå è¿æ»¤èµæºæ件
/qing_mei_xiu/article/details/
HSSF XSSF SXSSF
1.å建工ä½ç°¿Workbook2.å建Sheet
3.å建è¡Row
4.å建åå æ ¼Cell
HSSFWorkbookæ¯æä½Excel以åï¼å æ¬ï¼ççæ¬ï¼æ©å±å为.xlsï¼æ以æ¯ä¸ªSheetå±éå°±æ¯å¯¼åºçè¡æ°è³å¤ä¸ºè¡ï¼ä¸è¬ä¸ä¼åçå åä¸è¶³çæ åµï¼OOMï¼ã
è¿ç§å½¢å¼çåºç°æ¯ç±äºHSSFWorkbookçå±éæ§è产ççï¼å ä¸ºå ¶æ导åºçè¡æ°æ¯è¾å°ï¼å¹¶ä¸åªé对Excel以åï¼å æ¬ï¼ççæ¬ççæ¬ï¼æ以 XSSFWookbookåºè¿èçï¼å ¶å¯¹åºçæ¯EXCEL以åççæ¬(è¡ï¼å)æ©å±å.xlsxï¼æ¯ä¸ªSheetæå¤å¯ä»¥å¯¼åºä¸è¡ï¼ä¸è¿è¿æ ·å°±ä¼´éçä¸ä¸ªOOMå å溢åºçé®é¢ï¼åå æ¯ä½ æå建çsheet row cell çæ¤æ¶æ¯åå¨å åä¸çï¼éçæ°æ®éå¢å¤§ ï¼å åçéæ±éä¹å°±å¢å¤§ï¼é£ä¹å¾å¤§å¯è½å°±æ¯è¦OOMäºã
ä»POI 3.8çæ¬å¼å§ï¼æä¾äºä¸ç§åºäºXSSFWorkbookçä½å åå ç¨çå·¥ä½ç°¿SXSSFWorkbookã
å¼ç¨å®æ¹çä»ç»ï¼ç®åæ¦æ¬å°±æ¯ï¼
SXSSFæ¯å¯¹XSSFçä¸ç§æµå¼æ©å±ï¼ç¹ç¹æ¯éç¨äºæ»å¨çªå£çæºå¶ï¼ä½å åå ç¨ï¼ä¸»è¦ç¨äºæ°æ®éé常大ççµåè¡¨æ ¼èèææºå æéçæ åµã
åçæ¯å©ç¨äºæ»å¨çªå£æºå¶ã
SXSSFWorkbook.DEFAULT_WINDOW_SIZEé»è®¤å¼æ¯ï¼è¡¨ç¤ºå¨å åä¸æå¤åå¨ä¸ªRow对象ï¼å½å第个Row对象çæ¶åå°±ä¼æ第1个Row对象以XMLæ ¼å¼åå ¥C:\Users\wange\AppData\Local\Tempè·¯å¾ä¸ç临æ¶æ件ä¸ï¼åé¢ç以æ¤ç±»æ¨ï¼å§ç»ä¿æå åä¸æå¤åå¨ä¸ªRow对象ã
SXSSFWorkbooké»è®¤ä½¿ç¨å èå符串èä¸æ¯ å ±äº«å符串表 (SharedStringsTable)ãå¯ç¨å ±äº«å符串æ¶ï¼ææ¡£ä¸çææå¯ä¸å符串é½å¿ é¡»ä¿åå¨å åä¸ï¼å æ¤ä¼å ç¨æ´å¤çå åã
ä¸XSSFç对æ¯ï¼å¨ä¸ä¸ªæ¶é´ç¹ä¸ï¼åªå¯ä»¥è®¿é®ä¸å®æ°éçRowï¼ä¸åæ¯æSheet.clone()ï¼ä¸åæ¯æå ¬å¼çæ±å¼ãä½æ¯é¤äºæ»å¨çªå£ï¼å ¶ä½çEXCLEæä½ä»ç¶ä½¿ç¨çæ¯XSSFçAPIã
å¦å¤å®æ¹æ示导åºEXCELååºè¯¥è°ç¨wb.dispose()æ¥å é¤ä¹åä¿åç临æ¶æ件ã
wb.write(out)éè¿æºç äºè§£å°è¿ç¨æ¯
1ãå°wbçææsheetè°ç¨flushRows()移åºå åï¼åå ¥ä¸´æ¶.xmlæ件ä¸
2ãçæäºä¸ä¸ªä¸´æ¶.xlsxæ件å°wbçä¸äºæ¨¡æ¿æ°æ®åå ¥è¿ä¸ªä¸´æ¶æ件
3ãå°è¿ä¸ªä¸´æ¶.xlsxæ件转æZipFileï¼éåææZipEntryæ¥è·åSheetï¼å¦æ没æSheetåç´æ¥å¤å¶æµã
4ãå¦æè½å¤è·åå°Sheetçåæ¯é£äºä¸´æ¶.xmlæ件ï¼å¨å¯¹è¿äºæ件è¿è¡è§£æ并追踪åå ¥å¯¼åºæ件ä¸ã
ï¼è¿è¾¹å¯è½æ¯æ¶åå°äºä¸äºEXCELæä»¶æ ¼å¼çåçï¼å°±ä¸æ·±å ¥ç 究äºï¼
SXSSFWorkbook wb = new SXSSFWorkbook(-1)
åå§å设置为-1çæ¶åæ们å¯ä»¥èªå·±å®ä¹å临æ¶æ件è§åï¼æ¯å¦æ¯è¯»è¡è®°å½flushå°ä¸´æ¶ä¸æ¬¡ï¼å¯ä»¥å¤§å¤§åå°ç£çIO次æ°ã
使ç¨SAX模åæ¥è§£æEXCELä¸åDOM模åä¸ä¸ææææ件å 容å è½½è¿å åï¼å®éè¡æ«æææ¡£ï¼ä¸è¾¹æ«æï¼ä¸è¾¹è§£æãæ以é£äºåªéè¦åé读åå 容çåºç¨ç¨åºå°±å¯ä»¥ä»SAX解æä¸åçï¼è¿å¯¹å¤§åææ¡£ç解ææ¯ä¸ªå·¨å¤§ä¼å¿ã
å¦ä½ç¨Apache POI读åExcel
ããé¦å POIæ¯å¼æºç»ç»Apacheåºåçä¸ä¸ªå¼æºjarå ï¼æä¾äºæ¹ä¾¿è§£æExcelçAPIï¼æ们å¯ä»¥é常æ¹ä¾¿ç使ç¨å®æ¥è¯»åExcelãè¿éä»ç»3.5Finalçæ¬ãããæéç¨å°çjarå å¦ä¸:
ãã说å°Excelï¼æåï¼æ ¼å¼æ¯ä¸ä¸æ ·çï¼ç¨POI解æçæ¹æ³ä¹å°±ä¸ä¸æ ·ï¼Excel主è¦æ¯ä½¿ç¨org.apache.poi.hssf.usermodelå ä¸çç±»æ¥è§£æï¼èExcelå°±æ¯ä½¿ç¨org.apache.poi.xssf.usermodelæ¥è§£æã
ãã解æExcelæºç
说å°Excelï¼æåï¼æ ¼å¼æ¯ä¸ä¸æ ·çï¼ç¨POI解æçæ¹æ³ä¹å°±ä¸ä¸æ ·ï¼Excel主è¦æ¯ä½¿ç¨org.apache.poi.hssf.usermodelå ä¸çç±»æ¥è§£æï¼èExcelå°±æ¯ä½¿ç¨org.apache.poi.xssf.usermodelæ¥è§£æã
解æExcelæºç
StringBuffer content = new StringBuffer();
HSSFWorkbook workbook = new HSSFWorkbook(is); // å建对Excelå·¥ä½ç°¿æ件çå¼ç¨
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets); // è·å¾ä¸ä¸ªsheet
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // è·å¾ä¸è¡
for (int cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow); // è·å¾åå¼
if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
content.append(aCell.getNumericCellValue() + "\t");
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
content.append(aCell.getBooleanCellValue() + "\t");
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
content.append(aCell.getStringCellValue() + "\t");
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
content.append(aCell.getCellFormula() + "\t");
} else {
continue;
}
}
if(cellNumOfRow == aRow.getLastCellNum()) {
content.append("\n");
}
}
}
}
}
}
return content.toString().trim();
解æExcelååºæ¬ä¸æ ·ï¼åªæ¯å°HSSFSheet,HSSFCellçé½æ¹æXSSFSheet,XSSFCellå³å¯ã
å¦å¤è¦æé大家çæ¯Excelçæ ·å¼é½æ¯åºäºä¸ä¸ªåå æ ¼çï¼æ以ç¨HSSFRow.getRowStyle()æ¿æ ·å¼ä¼åºé®é¢çï¼ä¸ä¸å®ä¼æ¿å°ä½ æ³è¦çæ ·å¼ãå¤çå并åå æ ¼æ¯POIçä¸ä¸ªé¾ç¹ï¼åªè½éè¿å¤æå½ååå æ ¼æ¯å¦å¨å并åå æ ¼ä¹ä¸ï¼å¦ææ¯ï¼é£æ¤åå æ ¼çå¼ä¾¿æ¯è¿ä¸ªå并åå æ ¼çé¦ä½ç½®åå æ ¼çå¼ï¼åªæéè¿è¿æ ·æè½æ¥å¤çå并åå æ ¼ã
å¤çå并åå æ ¼ç代ç ï¼
public static boolean isMergedRegion(HSSFSheet sheet, HSSFCell cell) {
//å¾å°ä¸ä¸ªsheetä¸æå¤å°ä¸ªå并åå æ ¼
int sheetmergerCount = sheet.getNumMergedRegions();
for(int i = 0; i < sheetmergerCount; i++) {
//å¾åºå ·ä½çå并åå æ ¼
CellRangeAddress ca = sheet.getMergedRegion(i);
//å¾å°å并åå æ ¼çèµ·å§è¡, ç»æè¡, èµ·å§å, ç»æå
int firstC = ca.getFirstColumn();
int lastC = ca.getLastColumn();
int firstR = ca.getFirstRow();
int lastR = ca.getLastRow();
//å¤æ该åå æ ¼æ¯å¦å¨å并åå æ ¼èå´ä¹å , å¦ææ¯, åè¿å true
if(cell.getColumnIndex() <= lastC && cell.getColumnIndex() >= firstC) {
if(cell.getRowIndex() <= lastR && cell.getRowIndex() >= firstR) {
return true;
}
}
}
return false;
}
public static String getMergedRegionValue(HSSFSheet sheet, HSSFCell cell) {
//è·å¾ä¸ä¸ª sheet ä¸å并åå æ ¼çæ°é
int sheetmergerCount = sheet.getNumMergedRegions();
//便å©å并åå æ ¼
for(int i = 0; i < sheetmergerCount; i++) {
//è·å¾å并åå æ ¼
CellRangeAddress ca = sheet.getMergedRegion(i);
//è·å¾å并åå æ ¼çèµ·å§è¡, ç»æè¡, èµ·å§å, ç»æå
int firstC = ca.getFirstColumn();
int lastC = ca.getLastColumn();
int firstR = ca.getFirstRow();
int lastR = ca.getLastRow();
//å¤æ该åå æ ¼æ¯å¦æ¯å¨å并åå æ ¼ä¸, å¦ææ¯, åè¿åæå¨å并åå æ ¼çé¦åå æ ¼çå¼
if(cell.getColumnIndex() <= lastC && cell.getColumnIndex() >= firstC) {
if(cell.getRowIndex() <= lastR && cell.getRowIndex() >= firstR) {
HSSFRow fRow = sheet.getRow(firstR);
HSSFCell fCell = fRow.getCell(firstC);
//é¤äºå并åå æ ¼é¦åå æ ¼çå¼, å ¶ä½çç¨(*)æ¥åºå
if (fCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC && hasBorder(cell)) {
return String.valueOf(fCell.getNumericCellValue());
} else if (fCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN && hasBorder(cell)) {
return String.valueOf(fCell.getBooleanCellValue());
} else if (fCell.getCellType() == HSSFCell.CELL_TYPE_STRING && hasBorder(cell)) {
return fCell.getStringCellValue();
} else if (fCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA && hasBorder(cell)){
return String.valueOf(fCell.getCellFormula());
}
}
}
}
return "";
}
2024-11-28 07:3596人浏览
2024-11-28 07:31635人浏览
2024-11-28 07:271424人浏览
2024-11-28 06:532987人浏览
2024-11-28 06:372894人浏览
2024-11-28 06:11493人浏览
1.html网页源码加密2.使用Cython加密Python代码防止反编译3.纯干货分享|源代码泄露的有效方法4.php源代码保护——PHP加密方案分析&解密还原5.Python源代码保密、加
1.linux内核通信核心技术:Netlink源码分析和实例分析2.商业源码源码分类3.那个网站有大量asp.net网站源码4.***.net是什么?5.请问什么是asp.net?linux内核通信核
1.Android Adb 源码分析(一)Android Adb 源码分析(一) 面对Android项目的调试困境,我们的团队在项目临近量产阶段,将userdebug版本切换为了user版本,并对