//操作excel package mytest; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Date; import java.util.UUID; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; 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.w3c.dom.Document; public class OperationExcel { public static void main(String[] args) throws IOException{ //添加两个学生到excel //addExcel("D:/test/operationExcel.xls"); //查询所有 /*try{ SelectExcel er=new SelectExcel("D:/test/operationExcel.xls"); String line=er.readLine(); while(line != null){ System.out.println("查询出来的内容是:"+line); line=er.readLine(); } er.close(); }catch(Exception e){ e.printStackTrace(); } */ //修改学生 //updateExcel("D:/test/operationExcel.xls"); //删除李四的信息 deleteExcel("D:/test/operationExcel.xls"); } //增加学生 public static void addExcel(String excelNamePath) throws IOException{ File oldFile = new File(excelNamePath); Document document = null; if (oldFile.exists()) { System.out.print("D:/test/operationExcel.xls文件存在"); } else { try { //不存在就创建一个xml文件 oldFile.createNewFile(); } catch (Exception e) { // TODO: handle exception System.out.print("不存在则创建,但是创建失败!"); } } //InputStream isExcel = new FileInputStream(excelNamePath); // 创建Excel的工作书册 Workbook,对应到一个excel文档 HSSFWorkbook wb = new HSSFWorkbook(); // 创建Excel的工作sheet,对应到一个excel文档的tab HSSFSheet sheet = wb.createSheet("sheet1"); try{ HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell=null; HSSFRow row1=null; String id = new String(UUID.randomUUID().toString()); row1 = sheet.createRow((short) 0);//开始第几行输出 cell = row1.createCell((short) 0);//在第几行输出 //cell = row1.createCell((short) 0); cell.setCellStyle(style); cell.setCellValue("id"); cell = row1.createCell((short) 1); cell.setCellStyle(style); cell.setCellValue("name"); cell = row1.createCell((short) 2); cell.setCellStyle(style); cell.setCellValue("age"); cell = row1.createCell((short) 3); cell.setCellStyle(style); cell.setCellValue("sex"); row1 = sheet.createRow((short) 1);//开始第几行输出 cell = row1.createCell((short) 0);//在第几行输出 cell.setCellStyle(style); cell.setCellValue((int) 1); cell = row1.createCell((short) 1); cell.setCellStyle(style); cell.setCellValue("张三"); cell = row1.createCell((short) 2); cell.setCellStyle(style); cell.setCellValue((int) 18); cell = row1.createCell((short) 3); cell.setCellStyle(style); cell.setCellValue("女"); row1 = sheet.createRow((short) 2);//开始第几行输出 cell = row1.createCell((short) 0);//在第几行输出 cell.setCellStyle(style); cell.setCellValue((int) 2); cell = row1.createCell((short) 1); cell.setCellStyle(style); cell.setCellValue("李四"); cell = row1.createCell((short) 2); cell.setCellStyle(style); cell.setCellValue((int) 23); cell = row1.createCell((short) 3); cell.setCellStyle(style); cell.setCellValue("男"); FileOutputStream fout = new FileOutputStream(excelNamePath); wb.write(fout); System.out.println("文件已写入!"); fout.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block\ e.printStackTrace(); System.out.println("文件写入异常!"); } //最后调用查询方法 try{ SelectExcel er=new SelectExcel("D:/test/operationExcel.xls"); String line=er.readLine(); while(line != null){ System.out.println("查询出来的内容是:"+line); line=er.readLine(); } er.close(); }catch(Exception e){ e.printStackTrace(); } } //查询学生 private BufferedReader reader = null; private String filetype; private InputStream is = null; private int currSheet; private int currPosition; private int numOfSheets; HSSFWorkbook workbook = null; private static String EXCEL_LINE_DELIMITER = " "; private static int MAX_EXCEL_COLUMNS = 64; public void SelectExcel(String inputfile) throws IOException, Exception{ if (inputfile == null || inputfile.trim().equals("")){ throw new IOException("no input file specified"); } this.filetype = inputfile.substring(inputfile.lastIndexOf(".") + 1); currPosition = 0; currSheet = 0; is = new FileInputStream(inputfile); if (filetype.equalsIgnoreCase("txt")){ reader = new BufferedReader(new InputStreamReader(is)); } else if (filetype.equalsIgnoreCase("xls")){ workbook = new HSSFWorkbook(is); numOfSheets = workbook.getNumberOfSheets(); } else{ throw new Exception("File Type Not Supported"); } } public String readLine() throws IOException{ if (filetype.equalsIgnoreCase("txt")){ String str = reader.readLine(); while (str.trim().equals("")){ str = reader.readLine(); } return str; } else if (filetype.equalsIgnoreCase("xls")){ HSSFSheet sheet = workbook.getSheetAt(currSheet); if (currPosition > sheet.getLastRowNum()){ currPosition = 0; while (currSheet != numOfSheets - 1){ sheet = workbook.getSheetAt(currSheet + 1); if (currPosition == sheet.getLastRowNum()){ currSheet++; continue; } else{ int row = currPosition; currPosition++; return getLine(sheet, row); } } return null; } int row = currPosition; currPosition++; return getLine(sheet, row); } return null; } private String getLine(HSSFSheet sheet, int row){ HSSFRow rowline = sheet.getRow(row); StringBuffer buffer = new StringBuffer(); int filledColumns = rowline.getLastCellNum(); HSSFCell cell = null; for (int i = 0; i < filledColumns; i++){ cell = rowline.getCell((short) i); String cellvalue = null; if (cell != null){ switch (cell.getCellType()){ case HSSFCell.CELL_TYPE_NUMERIC:{ if (HSSFDateUtil.isCellDateFormatted(cell)){ Date date = cell.getDateCellValue(); cellvalue = cell.getDateCellValue().toLocaleString(); } else{ Integer num = new Integer((int) cell.getNumericCellValue()); cellvalue = String.valueOf(num); } break; } case HSSFCell.CELL_TYPE_STRING: cellvalue = cell.getStringCellValue().replaceAll("'", "''"); break; default: cellvalue = " "; } } else{ cellvalue = ""; } buffer.append(cellvalue).append(EXCEL_LINE_DELIMITER); System.out.println("buffer.toString()"+buffer.toString()); } return buffer.toString(); } public void close(){ if (is != null){ try{ is.close(); } catch (IOException e){ is = null; } } if (reader != null){ try{ reader.close(); } catch (IOException e){ reader = null; } } } //修改学生 public static void updateExcel(String excelNamePath){ int coloum = 2; // 比如你要获取第1列 try { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelNamePath)); HSSFSheet sheet = workbook.getSheet("Sheet1");//创建一个Sheet1 for (int i = 0; i <= sheet.getLastRowNum(); i++) { HSSFRow row = sheet.getRow(i); if (null == row) { continue; } else { if(row.getCell(coloum)!=null&&i==1){ HSSFCell cell = row.getCell((int) coloum); row.getCell(coloum).setCellType(cell.CELL_TYPE_STRING); cell.setCellValue("30"); //System.out.println(row.getCell(coloum).getStringCellValue()); coloum=3; HSSFCell cell1 = row.getCell((int) coloum); row.getCell(coloum).setCellType(cell.CELL_TYPE_STRING); cell1.setCellValue("未知"); //System.out.println(row.getCell(coloum).getStringCellValue()); } } } FileOutputStream out = null; try { out = new FileOutputStream(excelNamePath); workbook.write(out); } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //最后调用查询方法 try{ SelectExcel er=new SelectExcel("D:/test/operationExcel.xls"); String line=er.readLine(); while(line != null){ System.out.println("查询出来的内容是:"+line); line=er.readLine(); } er.close(); }catch(Exception e){ e.printStackTrace(); } } //删除学生 public static void deleteExcel(String excelNamePath){ int coloum = 0; // 比如你要获取第1列 try { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelNamePath)); HSSFSheet sheet = workbook.getSheet("Sheet1");//创建一个Sheet1 for (int i = 0; i <= sheet.getLastRowNum(); i++) { HSSFRow row = sheet.getRow(i); if (null == row) { continue; } else { if(row.getCell(coloum)!=null&&i==2){ HSSFCell cell = row.getCell((int) coloum); row.getCell(coloum).setCellType(cell.CELL_TYPE_STRING); //cell.setCellValue("30"); //System.out.println(row.getCell(coloum).getStringCellValue()); HSSFRow row1 = sheet.getRow(i); sheet.removeRow(row1); } } } FileOutputStream out = null; try { out = new FileOutputStream(excelNamePath); workbook.write(out); } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //最后调用查询方法 try{ SelectExcel er=new SelectExcel("D:/test/operationExcel.xls"); String line=er.readLine(); while(line != null){ System.out.println("查询出来的内容是:"+line); line=er.readLine(); } er.close(); }catch(Exception e){ e.printStackTrace(); } } } ----------------------------------------------------------------- package mytest; public class OprationExcelVO { private String id; private String name; private String age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } ================================================== //操作xml package mytest; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.UUID; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class Operationxml { public static void main(String[] args){ //添加两个学生到XML /*addStudentXml("D:/test/operationxml.xml","张三","20","男"); addStudentXml("D:/test/operationxml.xml","李四","21","女"); updateStudentXml("D:/test/operationxml.xml","30","未知"); deleteStudentXml("D:/test/operationxml.xml","df23be84-167b-4932-9875-3f07b4d3e264");*/ queryStudentXml("D:/test/operationxml.xml"); } //增加学生 public static boolean addStudentXml(String xmlName,String name,String age,String sex){ File oldFile = new File(xmlName); Document document = null; if (oldFile.exists()) { System.out.print("D:/test/operationxml.xml文件存在"); } else { try { //不存在就创建一个xml文件 oldFile.createNewFile(); } catch (Exception e) { // TODO: handle exception System.out.print("不存在则创建,但是创建失败!"); } } try{ //读取传入的路径,返回一个document对象,获取到DOM 解析器的工厂实例 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //获取一个DocumentBuider对象,调用newDocumentBuilder()方法得到创建 DOM 解析器的工厂 DocumentBuilder buider = factory.newDocumentBuilder(); /* * xml文档进行解析, * 调用 DOM 解析器对象的 parse() 方法 * 解析 XML 文档, * 得到代表整个文档的 Document 对象, * 进行可以利用DOM特性对整个XML文档进行操作了。 */ document = buider.parse(new File(xmlName)); //可以去掉xml文档中作为格式化内容的空白二映射在DOM树中 的不必要的TextNode对象 document.normalize(); /* * 写入信息 */ Element eltColor = document.createElement("content"); //创建叶节点的第一个元素 Element eltNumber = document.createElement("ID"); //创建叶节点的第二个元素 Element eltColorValue = document.createElement("name"); //创建叶节点的第三个元素 Element eltMinValue = document.createElement("age"); //创建叶节点的第四个元素 Element eltMaxValue = document.createElement("sex"); //创建叶节点的第一个元素下的文本节点 Text number_ = document.createTextNode(UUID.randomUUID().toString());//生成唯一随机识别码。 //Text number_ = document.createTextNode(id); //把该文本节点加入到叶节点的第一个元素里面 eltNumber.appendChild(number_); //创建叶节点的第二个元素下的文本节点 Text colorValue_ = document.createTextNode(name); //把该文本节点加入到叶节点的第二个元素里面 eltColorValue.appendChild(colorValue_); //创建叶节点的第三个元素下的文本节点\ Text minValue_ = document.createTextNode(age); //把该文本节点加入到叶节点的第二个元素里面 eltMinValue.appendChild(minValue_); //创建叶节点的第四个元素下的文本节点 Text maxValue_ = document.createTextNode(sex); //把该文本节点加入到叶节点的第四个元素里面 eltMaxValue.appendChild(maxValue_); //把叶节点下的元素加入到叶节点下 eltColor.appendChild(eltNumber); eltColor.appendChild(eltColorValue); eltColor.appendChild(eltMinValue); eltColor.appendChild(eltMaxValue); //获取根节点 Element eltRoot = document.getDocumentElement(); //把叶节点加入到根节点下 eltRoot.appendChild(eltColor); //更新修改后的源文件 saveXML(document,xmlName);//提交保存 return true; }catch(Exception e){ e.printStackTrace(); System.out.print(e.getMessage()); return false; } } /*保存数据到文件中...*/ private static boolean saveXML(Document document, String xmlName) { // TODO Auto-generated method stub try{ TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(xmlName)); transformer.transform(source, result); return true; }catch(Exception e){ e.printStackTrace(); System.out.println(e.getMessage()); return false; } } /* 修改学生信息 */ public static void updateStudentXml(String xmlName,String uage,String usex){ Document document = null; //读取传入的路径,返回一个document对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { //获取一个DocumentBuider对象 DocumentBuilder buider = factory.newDocumentBuilder(); //xml文档进行解析 document = buider.parse(new File(xmlName)); //可以去掉xml文档中作为格式化内容的空白二映射在DOM树中 的不必要的TextNode对象 document.normalize(); NodeList nodeList = document.getElementsByTagName("content"); String updateId = "490862cd-0cbb-4ffa-90ee-21e8b20af0c9"; for(int i=0;i<nodeList.getLength();i++){ String id = document.getElementsByTagName("ID").item(i).getFirstChild().getNodeValue(); String age = document.getElementsByTagName("age").item(i).getFirstChild().getNodeValue(); if(id.equals(updateId)){ document.getElementsByTagName("age").item(i).getFirstChild().setNodeValue(uage); document.getElementsByTagName("sex").item(i).getFirstChild().setNodeValue(usex); System.out.println("修改成功!"); } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); System.out.println("修改异常!"); } saveXML(document, xmlName); } public static void deleteStudentXml(String xmlName,String deleteNumber){ Document document = null; //读取传入的路径,返回一个document对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { //获取一个DocumentBuider对象 DocumentBuilder buider = factory.newDocumentBuilder(); //xml文档进行解析 document = buider.parse(new File(xmlName)); //可以去掉xml文档中作为格式化内容的空白二映射在DOM树中 的不必要的TextNode对象 document.normalize(); NodeList nodeList = document.getElementsByTagName("content"); for (int i = 0; i < nodeList.getLength(); i++) { String number_ = document.getElementsByTagName("ID").item(i).getFirstChild().getNodeValue(); // 删除节点时传入的参数 if (number_.equals(deleteNumber)) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); saveXML(document, xmlName); System.out.println("删除xml中该节点成功!"); } } } catch (Exception e) { // TODO: handle exception System.out.println("删除xml中该节点异常!"); } } public static void queryStudentXml(String xmlName){ List<OprationxmlVO> contentValueList = new ArrayList<OprationxmlVO>(); try{ //读取传入的路径,返回一个document对象 Document document = loadInit(xmlName); //获取叶节点 NodeList contentNodeList = document.getElementsByTagName("content"); //遍历叶节点 for(int i=0; i<contentNodeList.getLength(); i++){ /*Oprationxmlvo contentNode = new Oprationxmlvo();*/ String idNode = document.getElementsByTagName("ID").item(i).getFirstChild().getNodeValue(); String nameNode = document.getElementsByTagName("name").item(i).getFirstChild().getNodeValue(); String ageNode = document.getElementsByTagName("age").item(i).getFirstChild().getNodeValue(); String sexNade = document.getElementsByTagName("sex").item(i).getFirstChild().getNodeValue(); System.out.println("id="+idNode); System.out.println("name="+nameNode); System.out.println("age="+ageNode); System.out.println("sex="+sexNade); /*contentNode.setID(idNode); contentNode.setAge(ageNode); contentNode.setSex(sexNade); contentValueList.add(contentNode);*/ } }catch(Exception e){ e.printStackTrace(); System.out.println(e.getMessage()); } } public static Document loadInit(String filePath){ Document document = null; try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(new File(filePath)); document.normalize(); return document; }catch(Exception e){ e.printStackTrace(); System.out.println(e.getMessage()); return null; } } } ----------------------------------------------------------- package mytest; public class OprationxmlVO { private String ID; private String age; private String sex; public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } ====================================================== //操作txt package mytest; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class project{ public static void main(String[] args) { try { //第一 project yf = new project(); yf.yidong_File(); } catch (Exception e) { // TODO: handle exception }finally{ System.out.println("操作成功1"); } try { //第二 for(int i = 0;i<10;i++){ move_File("D:/test/move_File"+i+".txt"); } } catch (Exception e) { // TODO: handle exception }finally{ System.out.println("操作成功2"); } try { //第三 del_move_File("D:/test/move_File_delete.txt"); } catch (Exception e) { // TODO: handle exception }finally{ System.out.println("操作删除成功3"); } try { //第四 write_move_File("D:/test/move_File_write.txt","在文件里生成内容。。。。。。就是我啦!!"); } catch (Exception e) { // TODO: handle exception }finally{ System.out.println("操作写入内容成功4"); } try { //第五 read_move_File("D:/test/move_File_write.txt"); } catch (Exception e) { // TODO: handle exception }finally{ System.out.println("操作读取内容成功5"); } } //移动一个.txt文件 public void yidong_File(){ //文件原地址 File oldFile = new File("D:/test.txt"); //文件新(目标)地址 String newPath = "E:/"; //new一个新文件夹 File fnewpath = new File(newPath); //判断文件夹是否存在\ if(!fnewpath.exists()){ fnewpath.mkdirs(); } File fnew = new File(newPath+oldFile.getName()); oldFile.renameTo(fnew); } //移动多个.txt文件 public static void move_File(String fileName){ //文件原地址 File oldFile = new File(fileName); //文件新(目标)地址 String newPath = "E:/test/"; //new一个新文件夹 File fnewpath = new File(newPath); //判断文件夹是否存在\ if(!fnewpath.exists()){ fnewpath.mkdirs(); } File fnew = new File(newPath+oldFile.getName()); oldFile.renameTo(fnew); } //删除.txt文件 public static void del_move_File(String fileName){ //文件原地址 File oldFile = new File(fileName); //文件新(目标)地址 String newPath = "E:/test/"; //new一个新文件夹 File fnewpath = new File(newPath); //判断文件夹是否存在\ if(!fnewpath.exists()){ fnewpath.mkdirs(); } File fnew = new File(newPath+oldFile.getName()); // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除 if (oldFile.exists() && oldFile.isFile()) { if (oldFile.delete()) { System.out.println("删除单个文件" + fileName + "成功!"); } else { System.out.println("删除单个文件" + fileName + "失败!"); } } else { System.out.println("删除单个文件失败:" + fileName + "不存在!"); } } //写入数据到.txt文件 public static void write_move_File(String fileName,String str) throws IOException{ //文件原地址 File oldFile = new File(fileName); //文件新(目标)地址 String newPath = "E:/test/"; //new一个新文件夹 File fnewpath = new File(newPath); //判断文件夹是否存在\ if(!fnewpath.exists()){ fnewpath.mkdirs(); } File fnew = new File(newPath+oldFile.getName()); if (oldFile.exists()) { System.out.print("文件存在"); } else { System.out.print("文件不存在"); try { oldFile.createNewFile(); } catch (Exception e) { // TODO: handle exception System.out.print("不存在则创建,但是创建失败!"); }finally{ System.out.print("不存在则创建,创建成功!"); } FileWriter fw = null; try { fw = new FileWriter(oldFile); fw.write(str); } catch (Exception e) { // TODO: handle exception System.out.print("写入内容失败!"); }finally{ System.out.print("写入内容成功!"); if (fw != null) { fw.close(); fw = null; } } } } //读取.txt文件 public static String read_move_File(String fileName) throws IOException{ String result=null; FileReader fileReader=null; BufferedReader bufferedReader=null; try{ fileReader=new FileReader(fileName); bufferedReader=new BufferedReader(fileReader); try{ String read=null; while((read=bufferedReader.readLine())!=null){ result=result+read; } }catch(Exception e){ e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); }finally{ if(bufferedReader!=null){ bufferedReader.close(); } if(fileReader!=null){ fileReader.close(); } } System.out.println("读取出来的文件内容是:"+"\r\n"+result); return result; } } ============================================ //查询excel表 package mytest; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Date; 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; public class SelectExcel{ private BufferedReader reader = null; private String filetype; private InputStream is = null; private int currSheet; private int currPosition; private int numOfSheets; HSSFWorkbook workbook = null; private static String EXCEL_LINE_DELIMITER = " "; private static int MAX_EXCEL_COLUMNS = 64; public SelectExcel(String inputfile) throws IOException, Exception{ if (inputfile == null || inputfile.trim().equals("")){ throw new IOException("no input file specified"); } this.filetype = inputfile.substring(inputfile.lastIndexOf(".") + 1); currPosition = 0; currSheet = 0; is = new FileInputStream(inputfile); if (filetype.equalsIgnoreCase("txt")){ reader = new BufferedReader(new InputStreamReader(is)); }else if (filetype.equalsIgnoreCase("xls")){ workbook = new HSSFWorkbook(is); numOfSheets = workbook.getNumberOfSheets(); }else{ throw new Exception("File Type Not Supported"); } } public String readLine() throws IOException{ if (filetype.equalsIgnoreCase("txt")){ String str = reader.readLine(); while (str.trim().equals("")){ str = reader.readLine(); } return str; }else if (filetype.equalsIgnoreCase("xls")){ HSSFSheet sheet = workbook.getSheetAt(currSheet); if (currPosition > sheet.getLastRowNum()){ currPosition = 0; while (currSheet != numOfSheets - 1){ sheet = workbook.getSheetAt(currSheet + 1); if (currPosition == sheet.getLastRowNum()){ currSheet++; continue; } else{ int row = currPosition; currPosition++; return getLine(sheet, row); } } return null; } int row = currPosition; currPosition++; return getLine(sheet, row); } return null; } private String getLine(HSSFSheet sheet, int row){ HSSFRow rowline = sheet.getRow(row); StringBuffer buffer = new StringBuffer(); int filledColumns = rowline.getLastCellNum(); HSSFCell cell = null; for (int i = 0; i < filledColumns; i++){ cell = rowline.getCell((short) i); String cellvalue = null; if (cell != null){ switch (cell.getCellType()){ case HSSFCell.CELL_TYPE_NUMERIC:{ if (HSSFDateUtil.isCellDateFormatted(cell)){ Date date = cell.getDateCellValue(); cellvalue = cell.getDateCellValue().toLocaleString(); } else{ Integer num = new Integer((int) cell.getNumericCellValue()); cellvalue = String.valueOf(num); } break; } case HSSFCell.CELL_TYPE_STRING: cellvalue = cell.getStringCellValue().replaceAll("'", "''"); break; default: cellvalue = " "; } } else{ cellvalue = ""; } buffer.append(cellvalue).append(EXCEL_LINE_DELIMITER); } return buffer.toString(); } public void close(){ if (is != null){ try{ is.close(); } catch (IOException e){ is = null; } } if (reader != null){ try{ reader.close(); } catch (IOException e){ reader = null; } } } /* public static void main(String[] args){ try{ SelectExcel er=new SelectExcel("D:/test/operationExcel.xls"); String line=er.readLine(); while(line != null){ System.out.println("line:"+line); line=er.readLine(); } er.close(); }catch(Exception e){ e.printStackTrace(); } } */ } ======================================== //完 ========================================
使用的jar包