我的代码,是从sf.net上down下来的vb.net的版本。该版本已经很久没有进行维护了。我将其转化到了C#版本。不过说实话,没有一个WYSWYG的ide(如excel的designer),这段代码没什么作用。写一个excel文件,太复杂了。。。

下面是SmartExcel.CS的代码,其他的代码,我都放在回复里面,请慢慢看。。。
鞠强 smart excelusing System;
鞠强 smart excel
using System.IO;
鞠强 smart excel
using System.Runtime.InteropServices;
鞠强 smart excel
using System.Reflection;
鞠强 smart excel
鞠强 smart excel
namespace Genersoft.Platform.Application.SmartExcel
}
posted on 2004-07-08 09:25 鞠强 阅读(9541) 评论(35)  编辑 收藏 引用 网摘 所属分类: GSP开发
鞠强 smart excel

Feedback

# re: 与灵感之源的vb.net对应的SmartExcel的C#版本 2004-07-08 09:26 juqiang
//这是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
}
}
  回复  更多评论

# re: 与灵感之源的vb.net对应的SmartExcel的C#版本 2004-07-08 09:26 juqiang
//这是SmartExcelException.cs的代码
using System;

namespace Genersoft.Platform.Application
{
public class SmartExcelOpeartionFileException : ApplicationException
{
public SmartExcelOpeartionFileException() : base("请首先调用CreateFile方法!")
{

}
}
}
  回复  更多评论

# re: 与灵感之源的vb.net对应的SmartExcel的C#版本 2004-07-08 09:27 juqiang
//这是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参数的处理,都被我去掉了。

相关文章:

  • 2021-08-01
  • 2021-10-05
  • 2021-11-06
  • 2021-08-20
  • 2021-08-23
  • 2021-07-07
  • 2021-06-21
  • 2021-11-09
猜你喜欢
  • 2021-11-14
  • 2021-08-09
  • 2021-08-27
  • 2021-04-04
  • 2021-10-24
  • 2022-12-23
相关资源
相似解决方案