在论坛发布的帖子,受到热心网友支持,翻译了两个语言的版本出来,在此一起收录
原文:
ypZhuang
网友的java代码
- import
java.io.*; - import
java.util.*; - public
class
ExcelWriter { - public
static
void
main(String args[]){ - try
{ - ExcelWriter excel =
new
ExcelWriter(
"c://mytest.xls"
); - excel.beginWrite();
- String head[] = {
"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++){ - GradePO grade =
new
GradePO(); - grade.setStuNumber(i);
- grade.setName(
"学生"
+i); - grade.setScore(
88
.8f + i); - gradeList.add(grade);
- }
- String fields[] = {
"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(
"write file ok"
); - }
catch
(FileNotFoundException e) { - System.err.print(e.getMessage());
- e.printStackTrace();
- }
catch
(IOException e) { - System.err.print(e.getMessage());
- e.printStackTrace();
- }
catch
(Exception e) { - System.err.print(e.getMessage());
- e.printStackTrace();
- }
- }
- private
FileOutputStream _wirter; - private
int
row =
0
; - private
String path; - public
ExcelWriter(String strPath)
throws
FileNotFoundException { - _wirter =
new
FileOutputStream(strPath); - path = strPath;
- }
- /**
- * 写入short数组
- * @param values
- * @throws IOException
- */
- private
void
_writeFile(
short
[] values)
throws
IOException { - for
(
short
v : values) { - byte
[] b = getBytes(v); - _wirter.write(b,
0
, b.length); - }
- }
- /**
- * 写文件头
- * @throws IOException
- */
- public
void
beginWrite()
throws
IOException { - _writeFile(
new
short
[] {
0x809
,
8
,
0
,
0x10
,
0
,
0
}); - }
- /**
- * 写文件尾
- * @throws IOException
- */
- public
void
endWrite()
throws
IOException { - _writeFile(
new
short
[] {
0xa
,
0
}); - _wirter.close();
- }
-
/** - * 写一个浮点数到单元格x,y
- * @param x
- * @param y
- * @param value
- * @throws IOException
- */
- public
void
writeNumber(
short
x,
short
y,
float
value)
throws
IOException { - // _writeFile(new short[] { 0x203, 14, x, y, 0 });
- // byte[] b = getBytes(value);
- // _wirter.write(b, 0, b.length);
- writeString((
short
)x,(
short
)y,value+
""
); - }
- /**
- * 写一个数字到单元格x,y
- * @param x
- * @param y
- * @param value
- * @throws IOException
- */
- public
void
writeNumber(
int
x,
int
y,
float
value)
throws
IOException { - writeNumber((
short
)x,(
short
)y,value); - }
- /**
- * 写一个字符到单元格x,y
- * @param x
- * @param y
- * @param value
- * @throws IOException
- */
- public
void
writeString(
short
x,
short
y, String value)
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
- * @param x
- * @param y
- * @param value
- * @throws IOException
- */
- public
void
writeString(
int
x,
int
y, String value)
throws
IOException { - writeString((
short
)x,(
short
)y,value); - }
- /**
- * 写一个整数到单元格x,y
- * @param x
- * @param y
- * @param value
- * @throws IOException
- */
- public
void
writeNumber(
short
x,
short
y,
int
value)
throws
IOException { - // _writeFile(new short[] { 0x203, 14, x, y, 0 });
- // byte[] b = getBytes(value);
- // _wirter.write(b, 0, b.length);
- writeString(x,y,value+
""
); - }
- /**
- * 写一个整数到单元格x,y
- * @param x
- * @param y
- * @param value
- * @throws IOException
- */
- public
void
writeNumber(
int
x,
int
y,
int
value)
throws
IOException { - writeNumber((
short
)x,(
short
)y,value); - }
- /**
- * 写一个双精度浮点数到单元格x,y
- * @param x
- * @param y
- * @param value
- * @throws IOException
- */
- public
void
writeNumber(
short
x,
short
y,
double
value)
throws
IOException { - writeString(x,y,value+
""
); - }
- /**
- * 写一个双精度浮点数到单元格x,y
- * @param x
- * @param y
- * @param value
- * @throws IOException
- */
- public
void
writeNumber(
int
x,
int
y,
double
value)
throws
IOException { - writeNumber((
short
)x,(
short
)y,value); - }
-
/** - * row行写入一行字符串
- * @param rows
- * @param head
- * @throws IOException
- */
- public
void
addLine(
int
rows,String head[])
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行写入一行字符串
- * @param head 字符数组
- * @throws IOException
- */
- public
void
addLine(String head[])
throws
IOException{ - addLine(
0
,head); - }
- /**
- * 在row行写入一行字符串
- *
- * @param rows
- * @param list 字符LIST
- * @throws IOException
- */
- 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
; - }
- /**
- * 在当前行写入一行字符串
- * @param list
- * @throws IOException
- */
- public
void
addLine(java.util.List <String> list)
throws
IOException{ - addLine(row,list);
- }
- /**
- * 在当前行开始写入JavaBean对象List
- * @param beans
- * @param fields
- * @throws Exception
- */
- public
void
addBean(java.util.List beans, String fields[])
throws
Exception{ - String methodName =
null
; - Object params[] =
new
Object[
0
]; - Class paramCls[] =
new
Class[
0
]; - List <String> list =
new
ArrayList <String>(); - for
(Iterator iterator = beans.iterator(); iterator.hasNext();) { - Object obj = iterator.next();
- int
l = fields.length; - for
(
int
j =
0
; j < l; j++) { - String field = fields[j];
- methodName = (
new
StringBuilder(
"get"
)).append( - field.substring(
0
,
1
).toUpperCase()).append( - field.substring(
1
)).toString(); - String tmp = 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 = new byte[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);
- // return b;
- // return getBytes((short)n);
- return
getBytes(n +
""
); - }
- private
byte
[] getBytes(
float
f) { - return
getBytes(Float.floatToRawIntBits(f)); - }
- private
byte
[] getBytes(String s) { - return
s.getBytes(); - }
- public
InputStream getInputStreamResult()
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> -
''' <param name="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> -
''' <param name="x"></param> -
''' <param name="y"></param> -
'''<param name="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> -
''' <param name="x"></param> -
'''<param name="y"></param> -
'''<param name="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