在论坛发布的帖子,受到热心网友支持,翻译了两个语言的版本出来,在此一起收录
原文:
ypZhuang 网友的java代码
- import java.io.*;
- import java.util.*;
- public class ExcelWriter{
- public static void main(Stringargs[]){
- try {
- ExcelWriterexcel= new ExcelWriter( "c://mytest.xls" );
- excel.beginWrite();
- Stringhead[]={ "StuNumber" , "Name" , "Score" };
- excel.addLine(head);
- List<String>list= new ArrayList<String>();
- list.add( "99" );
- list.add( "jinjazz" );
- list.add( "99.9" );
- excel.addLine( 1 ,list);
- java.util.List<GradePO>gradeList= new ArrayList<GradePO>();
- for ( int i= 0 ;i< 10 ;i++){
- GradePOgrade= new GradePO();
- grade.setStuNumber(i);
- grade.setName( "学生" +i);
- grade.setScore( 88 .8f+i);
- gradeList.add(grade);
- }
- Stringfields[]={ "stuNumber" , "name" , "score" };
- excel.addBean(gradeList,fields);
- excel.writeNumber( 12 , 0 , 12 );
- excel.writeString( 12 , 1 , "ypzhuang" );
- excel.writeNumber( 12 , 2 , 100 .0d);
- excel.endWrite();
- System.out.println( "writefileok" );
- } catch (FileNotFoundExceptione){
- System.err.print(e.getMessage());
- e.printStackTrace();
- } catch (IOExceptione){
- System.err.print(e.getMessage());
- e.printStackTrace();
- } catch (Exceptione){
- System.err.print(e.getMessage());
- e.printStackTrace();
- }
- }
- private FileOutputStream_wirter;
- private int row= 0 ;
- private Stringpath;
- public ExcelWriter(StringstrPath) throws FileNotFoundException{
- _wirter= new FileOutputStream(strPath);
- path=strPath;
- }
- /**
- *写入short数组
- *@paramvalues
- *@throwsIOException
- */
- private void _writeFile( short []values) throws IOException{
- for ( short v:values){
- byte []b=getBytes(v);
- _wirter.write(b, 0 ,b.length);
- }
- }
- /**
- *写文件头
- *@throwsIOException
- */
- public void beginWrite() throws IOException{
- _writeFile( new short []{ 0x809 , 8 , 0 , 0x10 , 0 , 0 });
- }
- /**
- *写文件尾
- *@throwsIOException
- */
- public void endWrite() throws IOException{
- _writeFile( new short []{ 0xa , 0 });
- _wirter.close();
- }
- /**
- *写一个浮点数到单元格x,y
- *@paramx
- *@paramy
- *@paramvalue
- *@throwsIOException
- */
- public void writeNumber( short x, short y, float value) throws IOException{
- //_writeFile(newshort[]{0x203,14,x,y,0});
- //byte[]b=getBytes(value);
- //_wirter.write(b,0,b.length);
- writeString(( short )x,( short )y,value+ "" );
- }
- /**
- *写一个数字到单元格x,y
- *@paramx
- *@paramy
- *@paramvalue
- *@throwsIOException
- */
- public void writeNumber( int x, int y, float value) throws IOException{
- writeNumber(( short )x,( short )y,value);
- }
- /**
- *写一个字符到单元格x,y
- *@paramx
- *@paramy
- *@paramvalue
- *@throwsIOException
- */
- public void writeString( short x, short y,Stringvalue) throws IOException{
- byte []b=getBytes(value);
- _writeFile( new short []{ 0x204 ,( short )(b.length+ 8 ),x,y, 0 ,( short )b.length});
- _wirter.write(b, 0 ,b.length);
- }
- /**
- *写一个字符到单元格x,y
- *@paramx
- *@paramy
- *@paramvalue
- *@throwsIOException
- */
- public void writeString( int x, int y,Stringvalue) throws IOException{
- writeString(( short )x,( short )y,value);
- }
- /**
- *写一个整数到单元格x,y
- *@paramx
- *@paramy
- *@paramvalue
- *@throwsIOException
- */
- public void writeNumber( short x, short y, int value) throws IOException{
- //_writeFile(newshort[]{0x203,14,x,y,0});
- //byte[]b=getBytes(value);
- //_wirter.write(b,0,b.length);
- writeString(x,y,value+ "" );
- }
- /**
- *写一个整数到单元格x,y
- *@paramx
- *@paramy
- *@paramvalue
- *@throwsIOException
- */
- public void writeNumber( int x, int y, int value) throws IOException{
- writeNumber(( short )x,( short )y,value);
- }
- /**
- *写一个双精度浮点数到单元格x,y
- *@paramx
- *@paramy
- *@paramvalue
- *@throwsIOException
- */
- public void writeNumber( short x, short y, double value) throws IOException{
- writeString(x,y,value+ "" );
- }
- /**
- *写一个双精度浮点数到单元格x,y
- *@paramx
- *@paramy
- *@paramvalue
- *@throwsIOException
- */
- public void writeNumber( int x, int y, double value) throws IOException{
- writeNumber(( short )x,( short )y,value);
- }
- /**
- *row行写入一行字符串
- *@paramrows
- *@paramhead
- *@throwsIOException
- */
- public void addLine( int rows,Stringhead[]) throws IOException{
- if (rows< 0 ){
- rows= 0 ;
- }
- for ( int i= 0 ;head!= null &&i<head.length;i++){
- writeString(rows,i,head[i]);
- }
- row=rows+ 1 ;
- }
- /**
- *在第0行写入一行字符串
- *@paramhead字符数组
- *@throwsIOException
- */
- public void addLine(Stringhead[]) throws IOException{
- addLine( 0 ,head);
- }
- /**
- *在row行写入一行字符串
- *
- *@paramrows
- *@paramlist字符LIST
- *@throwsIOException
- */
- public void addLine( int rows,java.util.List<String>list) throws IOException{
- if (rows< 0 ){
- rows= 0 ;
- }
- for ( int i= 0 ;list!= null &&i<list.size();i++){
- writeString(rows,i,list.get(i));
- }
- row=rows+ 1 ;
- }
- /**
- *在当前行写入一行字符串
- *@paramlist
- *@throwsIOException
- */
- public void addLine(java.util.List<String>list) throws IOException{
- addLine(row,list);
- }
- /**
- *在当前行开始写入JavaBean对象List
- *@parambeans
- *@paramfields
- *@throwsException
- */
- public void addBean(java.util.Listbeans,Stringfields[]) throws Exception{
- StringmethodName= null ;
- Objectparams[]= new Object[ 0 ];
- ClassparamCls[]= new Class[ 0 ];
- List<String>list= new ArrayList<String>();
- for (Iteratoriterator=beans.iterator();iterator.hasNext();){
- Objectobj=iterator.next();
- int l=fields.length;
- for ( int j= 0 ;j<l;j++){
- Stringfield=fields[j];
- methodName=( new StringBuilder( "get" )).append(
- field.substring( 0 , 1 ).toUpperCase()).append(
- field.substring( 1 )).toString();
- Stringtmp=String.valueOf(obj.getClass().getMethod(methodName,paramCls).invoke(obj,params));
- list.add(tmp);
- }
- addLine(list);
- list.clear();
- }
- }
- private byte []getBytes( short n){
- byte []b= new byte [ 2 ];
- b[ 0 ]=( byte )(n& 0xff );
- b[ 1 ]=( byte )(n>> 8 & 0xff );
- return b;
- }
- private byte []getBytes( int n){
- //byte[]b=newbyte[4];
- //
- //b[0]=(byte)(n&0xff);
- //b[1]=(byte)(n>>8&0xff);
- //b[2]=(byte)(n>>16&0xff);
- //b[3]=(byte)(n>>24&0xff);
- //b[3]=(byte)(n&0xff);
- //b[2]=(byte)(n>>8&0xff);
- //b[1]=(byte)(n>>16&0xff);
- //b[0]=(byte)(n>>24&0xff);
- //returnb;
- //returngetBytes((short)n);
- return getBytes(n+ "" );
- }
- private byte []getBytes( float f){
- return getBytes(Float.floatToRawIntBits(f));
- }
- private byte []getBytes(Strings){
- return s.getBytes();
- }
- public InputStreamgetInputStreamResult() throws IOException{
- if (_wirter!= null ){
- endWrite();
- }
- return new FileInputStream(path);
- }
- }
hztltgg 网友的Vb.Net代码
- Public Class ExcelWriter
- Private _wirter As System.IO.FileStream
- Public Sub New ( ByVal strPath As String )
- _wirter= New System.IO.FileStream(strPath,System.IO.FileMode.OpenOrCreate)
- End Sub
- '''<summary>
- '''写入short数组
- '''</summary>
- '''<paramname="values"></param>
- Private Sub _writeFile( ByVal values As Short ())
- For Each v As Short In values
- Dim b As Byte ()=System.BitConverter.GetBytes(v)
- _wirter.Write(b,0,b.Length)
- Next
- End Sub
- '''<summary>
- '''写文件头
- '''</summary>
- Public Sub BeginWrite()
- _writeFile( New Short (){&H809,8,0,&H10,0,0})
- End Sub
- '''<summary>
- '''写文件尾
- '''</summary>
- Public Sub EndWrite()
- _writeFile( New Short (){&HA,0})
- _wirter.Close()
- End Sub
- '''<summary>
- '''写一个数字到单元格x,y
- '''</summary>
- '''<paramname="x"></param>
- '''<paramname="y"></param>
- '''<paramname="value"></param>
- Public Sub WriteNumber( ByVal x As Short , ByVal y As Short , ByVal value As Double )
- _writeFile( New Short (){&H203,14,x,y,0})
- Dim b As Byte ()=System.BitConverter.GetBytes(value)
- _wirter.Write(b,0,b.Length)
- End Sub
- '''<summary>
- '''写一个字符到单元格x,y
- '''</summary>
- '''<paramname="x"></param>
- '''<paramname="y"></param>
- '''<paramname="value"></param>
- Public Sub WriteString( ByVal x As Short , ByVal y As Short , ByVal value As String )
- Dim b As Byte ()=System.Text.Encoding. Default .GetBytes(value)
- _writeFile( New Short (){&H204, CShort ((b.Length+8)),x,y,0, CShort (b.Length)})
- _wirter.Write(b,0,b.Length)
- End Sub
- End Class