我的代码,是从sf.net上down下来的vb.net的版本。该版本已经很久没有进行维护了。我将其转化到了C#版本。不过说实话,没有一个WYSWYG的ide(如excel的designer),这段代码没什么作用。写一个excel文件,太复杂了。。。
下面是SmartExcel.CS的代码,其他的代码,我都放在回复里面,请慢慢看。。。
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;

namespace Genersoft.Platform.Application.SmartExcel
}
下面是SmartExcel.CS的代码,其他的代码,我都放在回复里面,请慢慢看。。。
Feedback
//这是SmartExcelEnumeration.CS的代码
using System;
namespace Genersoft.Platform.Application.SmartExcel
{
/// <summary>
/// 所有的枚举类型定义
/// </summary>
public enum ValueTypes
{
Integer = 0,
Number = 1,
Text = 2,
}
public enum CellAlignment
{
GeneralAlign = 0,
LeftAlign = 1,
CentreAlign = 2,
RightAlign = 3,
FillCell = 4,
LeftBorder = 8,
RightBorder = 16,
TopBorder = 32,
BottomBorder = 64,
Shaded = 128
}
// 'used by rgbAttr2
//'bits 0-5 handle the *picture* formatting, not bold/underline etc...
//'bits 6-7 handle the font number
public enum CellFont
{
Font0 = 0,
Font1 = 64,
Font2 = 128,
Font3 = 192,
}
// 'used by rgbAttr1
// 'bits 0-5 must be zero
// 'bit 6 locked/unlocked
// 'bit 7 hidden/not hidden
public enum CellHiddenLocked
{
Normal = 0,
Locked = 64,
Hidden = 128,
}
public enum MarginTypes
{
LeftMargin = 38,
RightMargin = 39,
TopMargin = 40,
BottomMargin = 41,
}
/// <summary>
///
/// </summary>
/// <remarks>可以使用|符号进行或组合</remarks>
public enum FontFormatting
{
NoFormat = 0,
Bold = 1,
Italic = 2,
Underline = 4,
Strikeout = 8
}
}
回复 更多评论
using System;
namespace Genersoft.Platform.Application.SmartExcel
{
/// <summary>
/// 所有的枚举类型定义
/// </summary>
public enum ValueTypes
{
Integer = 0,
Number = 1,
Text = 2,
}
public enum CellAlignment
{
GeneralAlign = 0,
LeftAlign = 1,
CentreAlign = 2,
RightAlign = 3,
FillCell = 4,
LeftBorder = 8,
RightBorder = 16,
TopBorder = 32,
BottomBorder = 64,
Shaded = 128
}
// 'used by rgbAttr2
//'bits 0-5 handle the *picture* formatting, not bold/underline etc...
//'bits 6-7 handle the font number
public enum CellFont
{
Font0 = 0,
Font1 = 64,
Font2 = 128,
Font3 = 192,
}
// 'used by rgbAttr1
// 'bits 0-5 must be zero
// 'bit 6 locked/unlocked
// 'bit 7 hidden/not hidden
public enum CellHiddenLocked
{
Normal = 0,
Locked = 64,
Hidden = 128,
}
public enum MarginTypes
{
LeftMargin = 38,
RightMargin = 39,
TopMargin = 40,
BottomMargin = 41,
}
/// <summary>
///
/// </summary>
/// <remarks>可以使用|符号进行或组合</remarks>
public enum FontFormatting
{
NoFormat = 0,
Bold = 1,
Italic = 2,
Underline = 4,
Strikeout = 8
}
}
回复 更多评论
//这是SmartExcelStructure.cs的代码
using System;
using System.Runtime.InteropServices;
namespace Genersoft.Platform.Application
{
/// <summary>
/// 读写EXCEL文件所需的所有结构体的定义
/// </summary>
///
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct FONT_RECORD
{
public short opcode;//49
public short length;//5+len(fontname)
public short FontHeight;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte FontAttributes1;//bit0 bold, bit1 italic, bit2 underline, bit3 strikeout, bit4-7 reserved
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte FontAttributes2;//reserved - always 0
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte FontNameLength;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct PASSWORD_RECORD
{
public short opcode;//47
public short length;//len(password)
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct HEADER_FOOTER_RECORD
{
public short opcode;//20 Header, 21 Footer
public short length;//1+len(text)
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte TextLength;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct PROTECT_SPREADSHEET_RECORD
{
public short opcode;//18
public short length;//2
public short Protect;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct FORMAT_COUNT_RECORD
{
public short opcode;//0x1f
public short length;//2
public short Count;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct FORMAT_RECORD
{
public short opcode;// 0x1e
public short length;//1+len(format)
[MarshalAs(UnmanagedType.U1, SizeConst=1)]public byte FormatLength;//len(format)
}//followed by the Format-Picture
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct COLWIDTH_RECORD
{
public short opcode;//36
public short length;//4
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte col1;//first column
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte col2;//last column
public short ColumnWidth;//at 1/256th of a character
}
// 'Beginning Of File record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct BEG_FILE_RECORD
{
public short opcode;
public short length;
public short version;
public short ftype;
}
// 'End Of File record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct END_FILE_RECORD
{
public short opcode;
public short length;
}
// 'true/false to print gridlines
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct PRINT_GRIDLINES_RECORD
{
public short opcode;
public short length;
public short PrintFlag;
}
// 'Integer record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct tInteger
{
public short opcode;
public short length;
public short row;//unsigned integer
public short col;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr1;//rgbAttr1 handles whether cell is hidden and/or locked
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr2;//rgbAttr2 handles the Font# and Formatting assigned to this cell
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr3;//rgbAttr3 handles the Cell Alignment/borders/shading
public short intValue;//the actual integer value
}
// 'Number record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct tNumber
{
public short opcode;
public short length;
public short row;
public short col;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr1;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr2;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr3;
public double NumberValue;//8 Bytes
}
//
// 'Label (Text) record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct tText
{
public short opcode;
public short length;
public short row;
public short col;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr1;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr2;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr3;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte TextLength;
}
//
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct MARGIN_RECORD_LAYOUT
{
public short opcode;
public short length;
public double MarginValue;//8 bytes
}
//
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct HPAGE_BREAK_RECORD
{
public short opcode;
public short length;
public short NumPageBreaks;
}
//
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct DEF_ROWHEIGHT_RECORD
{
public int opcode;
public int length;
public int RowHeight;
}
//
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct ROW_HEIGHT_RECORD
{
public int opcode;//08
public int length;//should always be 16 bytes
public int RowNumber;
public int FirstColumn;
public int LastColumn;
public int RowHeight;//written to file as 1/20ths of a point
public int internals;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte DefaultAttributes;//set to zero for no default attributes
public int FileOffset;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr1;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr2;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr3;
}
}
----------------------------------------------------------------------------
excel确实很小,对了,我忘记把C#的test代码放上来了,生成的excel才554字节。
下面是根据灵感老大的demo我用C#翻的代码,请看:
private void button1_Click(object sender, System.EventArgs e)
{
SmartExcel excel = new SmartExcel();
excel.CreateFile("c:\\test.xls");
excel.PrintGridLines = false;
double height = 1.5;
excel.SetMargin(MarginTypes.TopMargin, height);
excel.SetMargin(MarginTypes.BottomMargin, height);
excel.SetMargin(MarginTypes.LeftMargin, height);
excel.SetMargin(MarginTypes.RightMargin, height);
string font = "Arial";
short fontsize = 12;
excel.SetFont(font,fontsize,FontFormatting.Italic);
byte b1 = 1,b2 = 12;
short s3 = 18;
excel.SetColumnWidth(b1,b2,s3);
string header = "头";
string footer = "角";
excel.SetHeader(header);
excel .SetFooter(footer);
int row = 1,col = 1,cellformat = 0;
object title = "没有使用任何EXCEL组件,直接写成了一个EXCEL文件,cool吧?!";
excel.WriteValue(ValueTypes.Text,CellFont.Font0,CellAlignment.LeftAlign,CellHiddenLocked.Normal,row,col,title,cellformat);
excel.CloseFile();
}
对了,代码中原来的ref参数的处理,都被我去掉了。
using System;
using System.Runtime.InteropServices;
namespace Genersoft.Platform.Application
{
/// <summary>
/// 读写EXCEL文件所需的所有结构体的定义
/// </summary>
///
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct FONT_RECORD
{
public short opcode;//49
public short length;//5+len(fontname)
public short FontHeight;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte FontAttributes1;//bit0 bold, bit1 italic, bit2 underline, bit3 strikeout, bit4-7 reserved
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte FontAttributes2;//reserved - always 0
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte FontNameLength;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct PASSWORD_RECORD
{
public short opcode;//47
public short length;//len(password)
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct HEADER_FOOTER_RECORD
{
public short opcode;//20 Header, 21 Footer
public short length;//1+len(text)
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte TextLength;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct PROTECT_SPREADSHEET_RECORD
{
public short opcode;//18
public short length;//2
public short Protect;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct FORMAT_COUNT_RECORD
{
public short opcode;//0x1f
public short length;//2
public short Count;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct FORMAT_RECORD
{
public short opcode;// 0x1e
public short length;//1+len(format)
[MarshalAs(UnmanagedType.U1, SizeConst=1)]public byte FormatLength;//len(format)
}//followed by the Format-Picture
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct COLWIDTH_RECORD
{
public short opcode;//36
public short length;//4
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte col1;//first column
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte col2;//last column
public short ColumnWidth;//at 1/256th of a character
}
// 'Beginning Of File record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct BEG_FILE_RECORD
{
public short opcode;
public short length;
public short version;
public short ftype;
}
// 'End Of File record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct END_FILE_RECORD
{
public short opcode;
public short length;
}
// 'true/false to print gridlines
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct PRINT_GRIDLINES_RECORD
{
public short opcode;
public short length;
public short PrintFlag;
}
// 'Integer record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct tInteger
{
public short opcode;
public short length;
public short row;//unsigned integer
public short col;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr1;//rgbAttr1 handles whether cell is hidden and/or locked
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr2;//rgbAttr2 handles the Font# and Formatting assigned to this cell
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr3;//rgbAttr3 handles the Cell Alignment/borders/shading
public short intValue;//the actual integer value
}
// 'Number record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct tNumber
{
public short opcode;
public short length;
public short row;
public short col;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr1;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr2;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr3;
public double NumberValue;//8 Bytes
}
//
// 'Label (Text) record
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct tText
{
public short opcode;
public short length;
public short row;
public short col;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr1;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr2;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr3;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte TextLength;
}
//
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct MARGIN_RECORD_LAYOUT
{
public short opcode;
public short length;
public double MarginValue;//8 bytes
}
//
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct HPAGE_BREAK_RECORD
{
public short opcode;
public short length;
public short NumPageBreaks;
}
//
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct DEF_ROWHEIGHT_RECORD
{
public int opcode;
public int length;
public int RowHeight;
}
//
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
struct ROW_HEIGHT_RECORD
{
public int opcode;//08
public int length;//should always be 16 bytes
public int RowNumber;
public int FirstColumn;
public int LastColumn;
public int RowHeight;//written to file as 1/20ths of a point
public int internals;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte DefaultAttributes;//set to zero for no default attributes
public int FileOffset;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr1;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr2;
[MarshalAs(UnmanagedType.I1, SizeConst=1)]public byte rgbAttr3;
}
}
----------------------------------------------------------------------------
excel确实很小,对了,我忘记把C#的test代码放上来了,生成的excel才554字节。
下面是根据灵感老大的demo我用C#翻的代码,请看:
private void button1_Click(object sender, System.EventArgs e)
{
SmartExcel excel = new SmartExcel();
excel.CreateFile("c:\\test.xls");
excel.PrintGridLines = false;
double height = 1.5;
excel.SetMargin(MarginTypes.TopMargin, height);
excel.SetMargin(MarginTypes.BottomMargin, height);
excel.SetMargin(MarginTypes.LeftMargin, height);
excel.SetMargin(MarginTypes.RightMargin, height);
string font = "Arial";
short fontsize = 12;
excel.SetFont(font,fontsize,FontFormatting.Italic);
byte b1 = 1,b2 = 12;
short s3 = 18;
excel.SetColumnWidth(b1,b2,s3);
string header = "头";
string footer = "角";
excel.SetHeader(header);
excel .SetFooter(footer);
int row = 1,col = 1,cellformat = 0;
object title = "没有使用任何EXCEL组件,直接写成了一个EXCEL文件,cool吧?!";
excel.WriteValue(ValueTypes.Text,CellFont.Font0,CellAlignment.LeftAlign,CellHiddenLocked.Normal,row,col,title,cellformat);
excel.CloseFile();
}
对了,代码中原来的ref参数的处理,都被我去掉了。