using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
//不通过OLE生成excel文件的方法
ExcelWriter excel = new ExcelWriter(@"c:\test.xls");
excel.BeginWrite();
excel.WriteString(0, 0, "Name");
excel.WriteString(0, 1, "Score");
excel.WriteString(1, 0, "jinjazz");
excel.WriteNumber(1, 1, 100);
excel.WriteString(2, 0, "游客");
excel.WriteNumber(2, 1, 0);
excel.EndWrite();
}
}
public class ExcelWriter
{
System.IO.FileStream _wirter;
public ExcelWriter(string strPath)
{
_wirter = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate);
}
/// <summary>
/// 写入short数组
/// </summary>
/// <param name="values"></param>
private void _writeFile(short[] values)
{
foreach (short v in values)
{
byte[] b = System.BitConverter.GetBytes(v);
_wirter.Write(b, 0, b.Length);
}
}
/// <summary>
/// 写文件头
/// </summary>
public void BeginWrite()
{
_writeFile(new short[] { 0x809, 8, 0, 0x10, 0, 0 });
}
/// <summary>
/// 写文件尾
/// </summary>
public void EndWrite()
{
_writeFile(new short[] { 0xa, 0 });
_wirter.Close();
}
/// <summary>
/// 写一个数字到单元格x,y
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="value"></param>
public void WriteNumber(short x, short y, double value)
{
_writeFile(new short[] { 0x203, 14, x, y, 0 });
byte[] b = System.BitConverter.GetBytes(value);
_wirter.Write(b, 0, b.Length);
}
/// <summary>
/// 写一个字符到单元格x,y
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="value"></param>
public void WriteString(short x, short y, string value)
{
byte[] b = System.Text.Encoding.Default.GetBytes(value);
_writeFile(new short[] { 0x204, (short)(b.Length + 8), x, y,0, (short)b.Length });
_wirter.Write(b, 0, b.Length);
}
}
}
blog同步更新:
http://blog.csdn.net/jinjazz/archive/2008/08/01/2753869.aspx
67 个解决方案
#1
......牛x
对这都有研究
对这都有研究
#2
MARK
#3
强。thx
#4
mark
#5
强,顶下!
#6
顶!!!
#7
学习
#8
写入哪个SHEET呢?
#9
打开一个*.xls文件,然后在另存为 xml表格 (*.xml)文件,比如存为1.xml
然后将1.xml 改名为 1.xls....
以前项目要求导出excel文件,我都是这么搞的:
1.xls :
<table>
<tr>
<td>1</td>
</tr>
</table>
要什么字体,要什么宽度,加html标记就可以:)
然后将1.xml 改名为 1.xls....
以前项目要求导出excel文件,我都是这么搞的:
1.xls :
<table>
<tr>
<td>1</td>
</tr>
</table>
要什么字体,要什么宽度,加html标记就可以:)
#10
那么写入公式该咋办呢?
#11
xml格式的可以保存公式
#12
mark
#13
强,学习。
#14
支持,学习了.
#15
office的各种文件格式的相关规范已经公开可下载了,
有兴趣的朋友可以到MSDN网站去看看。
有兴趣的朋友可以到MSDN网站去看看。
#16
mark
#17
学习
#18
学习
#19
最简单的XLS格式,是文本格式: 列与列之间是TAB(9),行与行之间就回车换行,
导出的XLS格式就可用这种格式,快速简单,我都是这么作,唯一的只有字符,没有
其它属性。
导出的XLS格式就可用这种格式,快速简单,我都是这么作,唯一的只有字符,没有
其它属性。
#20
相当牛X..
#21
liu x
#22
请问能不能将xsl的数据文件生成excel结构的文件吗?
#23
ddd
#24
参考楼主的方法,我来个JAVA版本的,不过writeNumber上实现上有一点点瑕疵;同时增加addBean,addLine方法增强了这个类的功能!在做简单的EXCEL导出功能从此可摆脱jxl!
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);
}
}
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);
}
}
#25
up
#26
感谢24楼,不介意的话,我将收录到我的blog中
#27
楼主和24楼的都收了,以后用得着,省得自己写了。
#28
灭绝师太强烈关注
#29
收藏
#30
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
#31
收藏
#32
收藏
#33
#34
mark
#35
剪刀的贴子得收藏!
#36
mark
#37
当然没问题,参考你写的嘛!
writeNumber这个方法是由瑕疵的,
我不知道为什么用你的方法不行,我只是粗暴的把数字转换成String,然后调用writeString方法!
我不理解16进制是什么意思,也没去翻阅MSDN文档。
是JAVA和C#在数字类型上存储机制不一致相关?
楼主有什么高见?
#38
ypZhuang
http://topic.csdn.net/u/20080804/14/11dca96a-a910-47d1-bb6e-870e1e6f292d.html
领分去哦,100分哦
http://topic.csdn.net/u/20080804/14/11dca96a-a910-47d1-bb6e-870e1e6f292d.html
领分去哦,100分哦
#39
mark~
#40
楼主很强啊!·!!
支持
支持
#41
mark
#42
Java语言好
#43
zuo ge jihao
#44
为什么要这样做呢,不是可以直接调用VBA来操作OFFIC嘛。
能说说这样做的好处吗?
能说说这样做的好处吗?
#45
收藏............
#46
MARK
#47
好,学习
#48
8错8错
#49
太牛X了,楼主,我崇拜你!
#50
Nice work.
#1
......牛x
对这都有研究
对这都有研究
#2
MARK
#3
强。thx
#4
mark
#5
强,顶下!
#6
顶!!!
#7
学习
#8
写入哪个SHEET呢?
#9
打开一个*.xls文件,然后在另存为 xml表格 (*.xml)文件,比如存为1.xml
然后将1.xml 改名为 1.xls....
以前项目要求导出excel文件,我都是这么搞的:
1.xls :
<table>
<tr>
<td>1</td>
</tr>
</table>
要什么字体,要什么宽度,加html标记就可以:)
然后将1.xml 改名为 1.xls....
以前项目要求导出excel文件,我都是这么搞的:
1.xls :
<table>
<tr>
<td>1</td>
</tr>
</table>
要什么字体,要什么宽度,加html标记就可以:)
#10
那么写入公式该咋办呢?
#11
xml格式的可以保存公式
#12
mark
#13
强,学习。
#14
支持,学习了.
#15
office的各种文件格式的相关规范已经公开可下载了,
有兴趣的朋友可以到MSDN网站去看看。
有兴趣的朋友可以到MSDN网站去看看。
#16
mark
#17
学习
#18
学习
#19
最简单的XLS格式,是文本格式: 列与列之间是TAB(9),行与行之间就回车换行,
导出的XLS格式就可用这种格式,快速简单,我都是这么作,唯一的只有字符,没有
其它属性。
导出的XLS格式就可用这种格式,快速简单,我都是这么作,唯一的只有字符,没有
其它属性。
#20
相当牛X..
#21
liu x
#22
请问能不能将xsl的数据文件生成excel结构的文件吗?
#23
ddd
#24
参考楼主的方法,我来个JAVA版本的,不过writeNumber上实现上有一点点瑕疵;同时增加addBean,addLine方法增强了这个类的功能!在做简单的EXCEL导出功能从此可摆脱jxl!
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);
}
}
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);
}
}
#25
up
#26
感谢24楼,不介意的话,我将收录到我的blog中
#27
楼主和24楼的都收了,以后用得着,省得自己写了。
#28
灭绝师太强烈关注
#29
收藏
#30
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
#31
收藏
#32
收藏
#33
#34
mark
#35
剪刀的贴子得收藏!
#36
mark
#37
当然没问题,参考你写的嘛!
writeNumber这个方法是由瑕疵的,
我不知道为什么用你的方法不行,我只是粗暴的把数字转换成String,然后调用writeString方法!
我不理解16进制是什么意思,也没去翻阅MSDN文档。
是JAVA和C#在数字类型上存储机制不一致相关?
楼主有什么高见?
#38
ypZhuang
http://topic.csdn.net/u/20080804/14/11dca96a-a910-47d1-bb6e-870e1e6f292d.html
领分去哦,100分哦
http://topic.csdn.net/u/20080804/14/11dca96a-a910-47d1-bb6e-870e1e6f292d.html
领分去哦,100分哦
#39
mark~
#40
楼主很强啊!·!!
支持
支持
#41
mark
#42
Java语言好
#43
zuo ge jihao
#44
为什么要这样做呢,不是可以直接调用VBA来操作OFFIC嘛。
能说说这样做的好处吗?
能说说这样做的好处吗?
#45
收藏............
#46
MARK
#47
好,学习
#48
8错8错
#49
太牛X了,楼主,我崇拜你!
#50
Nice work.