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

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

原文:

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

ypZhuang 网友的java代码

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

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. '''<paramname="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. '''<paramname="x"></param>
  33. '''<paramname="y"></param>
  34. '''<paramname="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. '''<paramname="x"></param>
  44. '''<paramname="y"></param>
  45. '''<paramname="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

你可能感兴趣的:(java,.net,Excel,vb,VB.NET)