既然要 *** 作,请试一下下面的方法:
A, 用Excel打开要读取的excel文件。
B, 点击Excel菜单“文件”->“另存为”,选择文件类型为“CSV(逗号分隔)”。这样,原来的excel文件会被另存为csv格式的文件,该文件是文本文件,
可以用文本文件编辑工具打开的。原excel文件的每一行记录,被转换为一行文本了。一行记录各列的值,用逗号隔开了。
C, 用C语言的文件IO函数接口,读取上面文本文件即可。
The server encountered an internal error () that prevented it from fulfilling this request.package Read
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
import java.util.ArrayList
import java.util.List
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFCell
import org.apache.poi.xssf.usermodel.XSSFRow
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import bean.define
public class readxlsx {
public List<List<String>>readxl(String path) throws IOException{
InputStream is = new FileInputStream(path)
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is)
List<List<String>>result = new ArrayList<List<String>>()
/*
//Sheet xssfSheet = null
//循环每一页,并处理当前循环页*/
for (XSSFSheet xssfSheet: xssfWorkbook) {
if (xssfSheet == null)
continue
//处理当前页,循环读取每一行
for (int rowNum = 1rowNum <= xssfSheet.getLastRowNum()rowNum++){
XSSFRow xssfRow = xssfSheet.getRow(rowNum)
int minColIx = xssfRow.getFirstCellNum()
int maxColIx = xssfRow.getLastCellNum()
List<String>rowList = new ArrayList<String>()
//List<define>list = null
//遍历该行,获取处理每个cell元素
for (int colIx = minColIxcolIx <maxColIxcolIx++) {
XSSFCell cell = xssfRow.getCell(colIx)
if(cell == null) {
define define = new define()
XSSFCell id = xssfRow.getCell(0)
XSSFCell what = xssfRow.getCell(1)
define.setId(id.getStringCellValue())
define.setWhat(what.getStringCellValue())
continue
}
rowList.add(cell.toString())
}
result.add(rowList)
}
}
return result
}
}
package com.runoob.test
import java.io.IOException
import java.util.List
import java.io.PrintWriter
import java.sql.SQLException
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import Dao.dao
import bean.define
import Read.readxlsx
public class adden extends HttpServlet{
private define define
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8")
response.setContentType("text/htmlcharset=UTF-8")
readxlsx readxlsx = new readxlsx()
String path = request.getParameter("path")
PrintWriter out = response.getWriter()
List<List<String>>list = readxlsx.readxl(path)
String id = request.getParameter("id")
String what = request.getParameter("what")
List<String>de = new define()
dao d = new dao()
for(int i=0i<list.size()i++){
de=list.get(i)
if(dao.adden(define)){
response.sendRedirect("index.jsp")
}
else out.println("读取失败")
}
out.flush()
out.close()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)