• 通过文件结构直接生成xls文件的VB.Net和Java两个版本


    在论坛发布的帖子,受到热心网友支持,翻译了两个语言的版本出来,在此一起收录

    原文:

    通过文件结构直接生成xls文件

    ypZhuang 网友的java代码

    1. import  java.io.*; 
    2. import  java.util.*; 
    3. public   class  ExcelWriter { 
    4. public   static   void  main(String args[]){ 
    5. try  { 
    6. ExcelWriter excel =  new  ExcelWriter( "c://mytest.xls" ); 
    7.     excel.beginWrite(); 
    8.       
    9.             String head[] = { "StuNumber" , "Name" , "Score" }; 
    10.             excel.addLine(head);  
    11.             
    12.             List <String> list  =  new  ArrayList <String>(); 
    13.             list.add( "99" ); 
    14.             list.add( "jinjazz" ); 
    15.             list.add( "99.9" ); 
    16.             excel.addLine( 1 ,list); 
    17.             
    18.             java.util.List <GradePO> gradeList =  new  ArrayList <GradePO> (); 
    19.              for ( int  i= 0 ;i <  10  ; i++){ 
    20.             GradePO grade =  new  GradePO(); 
    21.             grade.setStuNumber(i); 
    22.             grade.setName( "学生" +i); 
    23.             grade.setScore( 88 .8f + i); 
    24.             gradeList.add(grade); 
    25.             } 
    26.             String fields[] = { "stuNumber" , "name" , "score" }; 
    27.             excel.addBean(gradeList, fields); 
    28.             
    29.             excel.writeNumber( 12 0 12 ); 
    30.             excel.writeString( 12 1 "ypzhuang" ); 
    31.             excel.writeNumber( 12 2 100 .0d); 
    32.             excel.endWrite(); 
    33.             
    34.             System.out.println( "write file ok" ); 
    35. catch  (FileNotFoundException e) { 
    36. System.err.print(e.getMessage()); 
    37. e.printStackTrace(); 
    38. catch  (IOException e) { 
    39. System.err.print(e.getMessage()); 
    40. e.printStackTrace(); 
    41. catch  (Exception e) { 
    42. System.err.print(e.getMessage()); 
    43. e.printStackTrace(); 
    44. private  FileOutputStream _wirter; 
    45. private   int  row =  0
    46. private  String path; 
    47. public  ExcelWriter(String strPath)  throws  FileNotFoundException { 
    48.   _wirter =  new  FileOutputStream(strPath); 
    49.   path = strPath; 
    50. /** 
    51. * 写入short数组 
    52. * @param values 
    53. * @throws IOException 
    54. */  
    55. private   void  _writeFile( short [] values)  throws  IOException { 
    56. for  ( short  v : values) { 
    57. byte [] b = getBytes(v); 
    58. _wirter.write(b,  0 , b.length); 
    59. /** 
    60. * 写文件头 
    61. * @throws IOException 
    62. */  
    63. public   void  beginWrite()  throws  IOException { 
    64. _writeFile( new   short [] {  0x809 8 0 0x10 0 0  }); 
    65. /** 
    66. * 写文件尾 
    67. * @throws IOException 
    68. */  
    69. public   void  endWrite()  throws  IOException { 
    70. _writeFile( new   short [] {  0xa 0  }); 
    71. _wirter.close(); 
    72.      /** 
    73.     * 写一个浮点数到单元格x,y 
    74.     * @param x 
    75.     * @param y 
    76.     * @param value 
    77.     * @throws IOException 
    78.     */  
    79. public   void  writeNumber( short  x,  short  y,  float  value)  throws  IOException { 
    80. // _writeFile(new short[] { 0x203, 14, x, y, 0 }); 
    81. // byte[] b = getBytes(value); 
    82. // _wirter.write(b, 0, b.length); 
    83. writeString(( short )x,( short )y,value+ "" ); 
    84. /** 
    85. * 写一个数字到单元格x,y 
    86. * @param x 
    87. * @param y 
    88. * @param value 
    89. * @throws IOException 
    90. */  
    91. public   void  writeNumber( int  x,  int  y,  float  value)  throws  IOException { 
    92. writeNumber(( short )x,( short )y,value); 
    93. /** 
    94. * 写一个字符到单元格x,y 
    95. * @param x 
    96. * @param y 
    97. * @param value 
    98. * @throws IOException 
    99. */  
    100. public   void  writeString( short  x,  short  y, String value)  throws  IOException { 
    101. byte [] b = getBytes(value); 
    102. _writeFile( new   short [] {  0x204 , ( short ) (b.length +  8 ), x, y,  0 ,( short ) b.length }); 
    103. _wirter.write(b,  0 , b.length); 
    104. /** 
    105. * 写一个字符到单元格x,y 
    106. * @param x 
    107. * @param y 
    108. * @param value 
    109. * @throws IOException 
    110. */  
    111. public   void  writeString( int  x,  int  y, String value)  throws  IOException { 
    112. writeString(( short )x,( short )y,value); 
    113. /** 
    114. * 写一个整数到单元格x,y 
    115. * @param x 
    116. * @param y 
    117. * @param value 
    118. * @throws IOException 
    119. */  
    120. public   void  writeNumber( short  x,  short  y,  int  value)  throws  IOException { 
    121. // _writeFile(new short[] { 0x203, 14, x, y, 0 }); 
    122. // byte[] b = getBytes(value); 
    123. // _wirter.write(b, 0, b.length); 
    124. writeString(x,y,value+ "" ); 
    125. /** 
    126. * 写一个整数到单元格x,y 
    127. * @param x 
    128. * @param y 
    129. * @param value 
    130. * @throws IOException 
    131. */  
    132. public   void  writeNumber( int  x,  int  y,  int  value)  throws  IOException { 
    133. writeNumber(( short )x,( short )y,value); 
    134. /** 
    135. * 写一个双精度浮点数到单元格x,y 
    136. * @param x 
    137. * @param y 
    138. * @param value 
    139. * @throws IOException 
    140. */  
    141. public   void  writeNumber( short  x,  short  y,  double  value)  throws  IOException { 
    142. writeString(x,y,value+ "" ); 
    143. /** 
    144. * 写一个双精度浮点数到单元格x,y 
    145. * @param x 
    146. * @param y 
    147. * @param value 
    148. * @throws IOException 
    149. */  
    150. public   void  writeNumber( int  x,  int  y,  double  value)  throws  IOException { 
    151. writeNumber(( short )x,( short )y,value); 
    152.      /** 
    153.     * row行写入一行字符串 
    154.     * @param rows 
    155.     * @param head 
    156.     * @throws IOException 
    157.     */  
    158. public    void  addLine( int  rows,String head[])  throws  IOException{ 
    159. if (rows <  0 ){ 
    160. rows =  0
    161. for ( int  i= 0 ;head!= null  && i < head.length;i++){ 
    162. writeString(rows,i,head[i]); 
    163. row = rows+ 1
    164. /** 
    165. * 在第0行写入一行字符串 
    166. * @param head 字符数组 
    167. * @throws IOException 
    168. */  
    169. public    void  addLine(String head[])  throws  IOException{ 
    170. addLine( 0 ,head); 
    171. /** 
    172. * 在row行写入一行字符串 
    173. * @param rows 
    174. * @param list 字符LIST 
    175. * @throws IOException 
    176. */  
    177. public   void  addLine( int  rows,java.util.List <String> list)  throws  IOException{ 
    178. if (rows <  0 ){ 
    179. rows =  0
    180. for ( int  i= 0 ;list!= null  && i <list.size();i++){ 
    181. writeString(rows,i,list.get(i)); 
    182. row = rows +  1
    183. /** 
    184. * 在当前行写入一行字符串 
    185. * @param list 
    186. * @throws IOException 
    187. */  
    188. public   void  addLine(java.util.List <String> list)  throws  IOException{ 
    189. addLine(row,list); 
    190. /** 
    191. * 在当前行开始写入JavaBean对象List 
    192. * @param beans 
    193. * @param fields 
    194. * @throws Exception 
    195. */  
    196. public   void  addBean(java.util.List beans, String fields[])  throws  Exception{ 
    197. String methodName =  null
    198. Object params[] =  new  Object[ 0 ]; 
    199. Class paramCls[] =  new  Class[ 0 ]; 
    200.         
    201. List <String> list =  new  ArrayList <String>(); 
    202. for  (Iterator iterator = beans.iterator(); iterator.hasNext();) { 
    203. Object obj = iterator.next(); 
    204. int  l = fields.length; 
    205. for  ( int  j =  0 ; j < l; j++) { 
    206. String field = fields[j]; 
    207. methodName = ( new  StringBuilder( "get" )).append( 
    208. field.substring( 0 1 ).toUpperCase()).append( 
    209. field.substring( 1 )).toString(); 
    210. String tmp = String.valueOf(obj.getClass().getMethod(methodName, paramCls).invoke(obj, params)); 
    211. list.add(tmp); 
    212. addLine(list); 
    213. list.clear(); 
    214. private    byte [] getBytes( short  n) { 
    215. byte [] b =  new   byte [ 2 ]; 
    216. b[ 0 ] = ( byte ) (n &  0xff ); 
    217. b[ 1 ] = ( byte ) (n >>  8  &  0xff ); 
    218. return  b; 
    219.     
    220. private    byte [] getBytes( int  n) { 
    221. // byte[] b = new byte[4]; 
    222. // 
    223. // b[0] = (byte) (n & 0xff); 
    224. // b[1] = (byte) (n >> 8 & 0xff); 
    225. // b[2] = (byte) (n >> 16 & 0xff); 
    226. // b[3] = (byte) (n >> 24 & 0xff); 
    227. // b[3] = (byte) (n & 0xff); 
    228. // b[2] = (byte) (n >> 8 & 0xff); 
    229. // b[1] = (byte) (n >> 16 & 0xff); 
    230. // b[0] = (byte) (n >> 24 & 0xff); 
    231. // return b; 
    232. // return getBytes((short)n); 
    233. return  getBytes(n +  "" ); 
    234. private    byte [] getBytes( float  f) { 
    235. return  getBytes(Float.floatToRawIntBits(f)); 
    236. private    byte [] getBytes(String s) { 
    237. return  s.getBytes(); 
    238. public  InputStream getInputStreamResult()  throws  IOException { 
    239. if (_wirter != null ){ 
    240. endWrite(); 
    241. return   new  FileInputStream(path); 
    242.     

    hztltgg 网友的Vb.Net代码

    1. Public   Class  ExcelWriter
    2.      Private  _wirter  As  System.IO.FileStream
    3.      Public   Sub   New ( ByVal  strPath  As   String )
    4.         _wirter =  New  System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate)
    5.      End   Sub
    6.      '''<summary>
    7.      '''写入short数组
    8.      ''' </summary>
    9.      ''' <param name="values"></param>
    10.      Private   Sub  _writeFile( ByVal  values  As   Short ())
    11.          For   Each  v  As   Short   In  values
    12.              Dim  b  As   Byte () = System.BitConverter.GetBytes(v)
    13.             _wirter.Write(b, 0, b.Length)
    14.          Next
    15.      End   Sub
    16.      '''<summary>
    17.      '''写文件头
    18.      '''</summary>
    19.      Public   Sub  BeginWrite()
    20.         _writeFile( New   Short () {&H809, 8, 0, &H10, 0, 0})
    21.      End   Sub
    22.      '''<summary>
    23.      ''' 写文件尾
    24.      ''' </summary>
    25.      Public   Sub  EndWrite()
    26.         _writeFile( New   Short () {&HA, 0})
    27.         _wirter.Close()
    28.      End   Sub
    29.      ''' <summary>
    30.      '''写一个数字到单元格x,y
    31.      ''' </summary>
    32.      ''' <param name="x"></param>
    33.      ''' <param name="y"></param>
    34.      '''<param name="value"></param>
    35.      Public   Sub  WriteNumber( ByVal  x  As   Short ByVal  y  As   Short ByVal  value  As   Double )
    36.         _writeFile( New   Short () {&H203, 14, x, y, 0})
    37.          Dim  b  As   Byte () = System.BitConverter.GetBytes(value)
    38.         _wirter.Write(b, 0, b.Length)
    39.      End   Sub
    40.      ''' <summary>
    41.      '''写一个字符到单元格x,y
    42.      ''' </summary>
    43.      ''' <param name="x"></param>
    44.      '''<param name="y"></param>
    45.      '''<param name="value"></param>
    46.      Public   Sub  WriteString( ByVal  x  As   Short ByVal  y  As   Short ByVal  value  As   String )
    47.          Dim  b  As   Byte () = System.Text.Encoding. Default .GetBytes(value)
    48.         _writeFile( New   Short () {&H204,  CShort ((b.Length + 8)), x, y, 0,  CShort (b.Length)})
    49.         _wirter.Write(b, 0, b.Length)
    50.      End   Sub
    51. End   Class
  • 相关阅读:
    【CF516D】Drazil and Morning Exercise(换根DP预处理+暴力双指针)
    【CF538G】Berserk Robot(思维)
    【CF521D】Shop(贪心)
    【洛谷4827】[国家集训队] Crash 的文明世界(斯特林数+换根DP)
    斯特林数的基础性质与斯特林反演的初步入门
    【CF566C】Logistical Questions(点分治)
    【CF980D】Perfect Groups(仔细一想是道水题)
    【洛谷2597】[ZJOI2012] 灾难(支配树)
    2020CCPC长春站题解A D F H J K
    2020CCPC长春站自我反省
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6204889.html
Copyright © 2020-2023  润新知