1、Select * from Sheet1$ ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); System.out.println (表格列数+numberOfColumns ); System.out.println( rsmd.getColumnName(1)+ , + rsmd.getColumnName(2) + + rsmd.getColumnName(3); while (rs.next() for (int i = 1; i 1) System.out.p
2、rint(, String columnValue = rs.getString(i); System.out.print(columnValue); System.out.println( rs.close(); st.close(); catch(Exception ex) System.err.print(Exception: System.err.println(ex.getMessage(); finally con.close();执行结果如下:2、 jxl.jarjxl.jar为开源代码,任何运行java虚拟机的操作系统都能使用这个jar包操作excel表格。不依赖其他任何第三方
3、的库。下载地址:mirrors.ibiblio.org/pub/mirrors/maven2/net/sourceforge/jexcelapi/jxl/2.6.10/jxl-2.6.10.jar程序如下:import java.io.File;import jxl.Sheet;import jxl.Workbook;/java -classpath .;E:eclipse3.6.2workspaceCrazyJavalibjxl.jar xuzhe.ExcelJXLpublic class ExcelJXL static String sourceFile = c:name.xls; / 源
4、文件 public static void main(String args) try Workbook book = Workbook.getWorkbook(new File(sourceFile); /0代表第一个工作表对象 Sheet sheet = book.getSheet(0); int rows = sheet.getRows(); int cols = sheet.getColumns(); String colname1 = sheet.getCell(0, 0).getContents().trim(); String colname2 = sheet.getCell(1
5、, 0).getContents().trim(); String colname3 = sheet.getCell(2, 0).getContents().trim(); System.out.println(colname1+colname2+colname3); for (int z = 1; z rows; z+) /0代表列数,z代表行数 String name = sheet.getCell(0, z).getContents(); String sex = sheet.getCell(1, z).getContents(); String ID = sheet.getCell(2
6、, z).getContents(); System.out.println(name+sex+ID); catch(Exception e) e.printStackTrace(); 3、 jcom.jarjcom.jar是日本人开发的,也是一个开源项目,下载地址:将jcom.jar拷贝到classlib目录下,将jcom.dll放到你的JAVA_HOME/bin目录下,否则会出现下面错误。import jp.ne.so_net.ga2.no_ji.jcom.excel8.*;import jp.ne.so_net.ga2.no_ji.jcom.*;import java.util.Date
7、;public class ExcelJCOM public static void main(String args) throws Exception JCOMReadExcel(); JCOMCreateExcel(); static void JCOMReadExcel() ReleaseManager rm = new ReleaseManager(); try System.out.println(EXCEL startup. / if already started, open new window ExcelApplication excel = new ExcelApplic
8、ation(rm); excel.Visible(true); String Filename = ExcelWorkbooks xlBooks = excel.Workbooks(); ExcelWorkbook xlBook = xlBooks.Open(Filename); ExcelWorksheets xlSheets = xlBook.Worksheets(); /第一个工作表 ExcelWorksheet xlSheet = xlSheets.Item(1); ExcelRange xlRange = xlSheet.Cells(); int i; int j; for(j=1;
9、j=4;j+) for(i=1;i=3;i+) System.out.print(xlRange.Item(j,i).Value(); if(i3) System.out.print( System.out.println( catch(Exception e) e.printStackTrace(); finally rm.release(); static void JCOMCreateExcel() try System.out.println( / if already started, open new window ExcelApplication excel = new Exce
10、lApplication(rm); excel.Visible(true); / display any informationVersion=+excel.Version();UserName=+excel.UserName();Caption=+excel.Caption();Value=+excel.Value(); ExcelWorkbooks xlBooks = excel.Workbooks(); ExcelWorkbook xlBook = xlBooks.Add(); / create new book / enumurate all files System.out.prin
11、tln (set infomation of files in current directory to cell . ExcelWorksheets xlSheets = xlBook.Worksheets(); ExcelWorksheet xlSheet = xlSheets.Item(1); ExcelRange xlRange = xlSheet.Cells(); xlRange.Item(1,1).Value(filename xlRange.Item(2,1).Value(size xlRange.Item(3,1).Value(last modified time xlRang
12、e.Item(4,1).Value(is directory xlRange.Item(5,1).Value(is file xlRange.Item(6,1).Value(can read xlRange.Item(7,1).Value(can write File path = new File(./ String filenames = path.list(); for(int i=0; ifilenames.length; File file = new File(filenamesi); System.out.println(file); xlRange.Item(1,i+2).Va
13、lue( file.getName() ); xlRange.Item(2,i+2).Value( (int)file.length() ); xlRange.Item(3,i+2).Value( new Date(file.lastModified() ); xlRange.Item(4,i+2).Value( file.isDirectory()?Yes:No xlRange.Item(5,i+2).Value( file.isFile()? xlRange.Item(6,i+2).Value( file.canRead()? xlRange.Item(7,i+2).Value( file
14、.canWrite()? char start = B char end = (char)(byte)start + filenames.length - 1);end= + end + String expression = =Sum(B2: + String.valueOf(end) + 2)expression= + expression + embed equation, calculate sum of filesize:+expression); xlRange.Item(1,filenames.length+2).Value(sum xlRange.Item(2,filename
15、s.length+2).Formula(expression); xlRange.Columns().AutoFit(); / fit columns / comment out, if print out. / output default printer. / System.out.println(print out. / xlSheet.PrintOut(); / comment out, if book save to file. / if no path, save to(My Documents) / System.out.println / (save to file. (My
16、Documents)testExcel.xls xlBook.SaveAs(testExcel.xls xlBook.Close(false,null,false); excel.Quit();thank you .4、 poi.jarpoi.jar是apache旗下的一个开源项目,下载地址:.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.8-20120326.zipimport org.apache.poi.hssf.usermodel.*;import java.io.FileInputStream;import java.io.F
17、ileOutputStream;import java.io.IOException;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;/public class ExcelPOI /* * param args * throws IOException * java xu
18、zhe.ExcelPOI */ SuppressWarnings(deprecation) public static void main(String args) throws IOException / TODO Auto-generated method stub ExcelPOI.POICreateExcel(); ExcelPOI.POIReadExcel(); public static void POICreateExcel() throws IOException HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = w
19、b.createSheet(new sheet /0行 HSSFRow row = sheet.createRow(short)0); /1列 row.createCell(short)1).setCellValue(HelloWorld FileOutputStream fileOut = new FileOutputStream(workbook.xls wb.write(fileOut); fileOut.close(); private static HSSFWorkbook readFile(String filename) throws IOException return new
20、 HSSFWorkbook(new FileInputStream(filename); public static void POIReadExcel() throws IOException String fileName = HSSFWorkbook wb = ExcelPOI.readFile(fileName);Data dump:n for (int k = 0; k wb.getNumberOfSheets(); k+) HSSFSheet sheet = wb.getSheetAt(k); int rows = sheet.getPhysicalNumberOfRows();S
21、heet + k + + wb.getSheetName(k) + has + rows + row(s). for (int r = 0; r r+) HSSFRow row = sheet.getRow(r); if (row = null) continue; int cells = row.getPhysicalNumberOfCells(); System.out.println(nROW + row.getRowNum() + + cells + cell(s). for (int c = 0; c cells; c+) HSSFCell cell = row.getCell(c)
22、; String value = null; switch (cell.getCellType() case HSSFCell.CELL_TYPE_FORMULA: value = FORMULA value= + cell.getCellFormula(); break; case HSSFCell.CELL_TYPE_NUMERIC:NUMERIC value= + cell.getNumericCellValue(); case HSSFCell.CELL_TYPE_STRING:STRING value= + cell.getStringCellValue(); default: System.out.println(CELL col= + cell.getColumnIndex() + VALUE= + value);以上就是我的一点心得体会,有误的地方请指正,来信发xuzhe_hn.。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1