jxl.dll操作总结
1)Jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作微软的Excel文档。除了Jxl之外,还有Apache的一个POI项目,也可以操作Excel,两者相比之下:Jxl使用方便,但功能相对POI比较弱。
2)jxl.dll据说是一个韩国人写的,并且是用java语言写的。是通过一个ikvm的工具转换为jxl.dll供windows平台使用的。具体可以参照下面两个链接内容:
http://www.splinter.com.au/using-java-libraries-in-a-c-app-with-ikvm/
http://blog.csdn.net/kingdax1/article/details/2679610
3)并且jxl不支持excel2007及以上的版本。
4)简要使用方法如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 using jxl; 6 using jxl.write; 7 namespace ExcelWrap 8 { 9 /// <summary> 10 /// jxl.dll帮助类 11 /// 说明:jxl不支持excel2007以上格式 12 /// </summary> 13 public class JxlHelper 14 { 15 16 public static void ReadExcel(string ExcelPath) 17 { 18 19 try { 20 21 //Create a workbook object from the file at specified location. 22 //Change the path of the file as per the location on your computer. 23 Workbook wrk1 = Workbook.getWorkbook(new FileInfo(ExcelPath)); 24 25 //Obtain the reference to the first sheet in the workbook 26 Sheet sheet1 = wrk1.getSheet(0); 27 28 //Obtain reference to the Cell using getCell(int col, int row) method of sheet 29 Cell colArow1 = sheet1.getCell(0, 0); 30 Cell colBrow1 = sheet1.getCell(1, 0); 31 Cell colArow2 = sheet1.getCell(0, 1); 32 33 //Read the contents of the Cell using getContents() method, which will return 34 //it as a String 35 String str_colArow1 = colArow1.getContents(); 36 String str_colBrow1 = colBrow1.getContents(); 37 String str_colArow2 = colArow2.getContents(); 38 39 //Display the cell contents 40 System.Console.WriteLine("Contents of cell Col A Row 1: \""+str_colArow1 + "\""); 41 System.Console.WriteLine("Contents of cell Col B Row 1: \""+str_colBrow1 + "\""); 42 System.Console.WriteLine("Contents of cell Col A Row 2: \"" + str_colArow2 + "\""); 43 44 45 } 46 catch (jxl.read.biff.BiffException e) 47 { 48 Console.WriteLine(e.StackTrace); 49 } catch (IOException e) { 50 Console.WriteLine(e.StackTrace); 51 } 52 53 } 54 55 public static void WriteExcel(string ExcelPath) 56 { 57 try { 58 FileInfo exlFile = new FileInfo(ExcelPath); 59 WritableWorkbook writableWorkbook = Workbook 60 .createWorkbook(exlFile); 61 62 WritableSheet writableSheet = writableWorkbook.createSheet( 63 "Sheet1", 0); 64 65 //Create Cells with contents of different data types. 66 //Also specify the Cell coordinates in the constructor 67 Label label = new Label(0, 0, "Label (String)"); 68 System.DateTime dt = System.DateTime.Now; 69 jxl.write.DateTime date = new jxl.write.DateTime(1, 0, ref dt); 70 jxl.write.Boolean bl = new jxl.write.Boolean(2, 0, true); 71 Number num = new Number(3, 0, 9.99); 72 73 //Add the created Cells to the sheet 74 writableSheet.addCell(label); 75 writableSheet.addCell(date); 76 writableSheet.addCell(bl); 77 writableSheet.addCell(num); 78 79 //Write and close the workbook 80 writableWorkbook.write(); 81 writableWorkbook.close(); 82 83 } catch (IOException e) { 84 Console.WriteLine(e.StackTrace); 85 } catch (jxl.write.biff.RowsExceededException e) { 86 Console.WriteLine(e.StackTrace); 87 } catch (WriteException e) { 88 Console.WriteLine(e.StackTrace); 89 } 90 } 91 } 92 }
5)其他的使用方法可以参考java实现的例子。网上关于java的例子还是挺多的。之前我并不知道它是通过java转换而来的,也没有可以使用的例子,只有通过Reflector反编译看源码,慢慢了解。
作者:小羽信息
出处:http://www.cnblogs.com/fer-team/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。