与灵感之源的vb.net对应的SmartExcel的C#版本

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

下面是SmartExcel.CS的代码,其他的代码,我都放在回复里面,请慢慢看。。。
None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Reflection;
None.gif
None.gif
namespace  Genersoft.Platform.Application.SmartExcel
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Excel读写类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SmartExcel
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//        'the memory copy API is used in the MKI$ function which converts an integer
InBlock.gif        
//        'value to a 2-byte string value to write to the file. (used by the Horizontal
InBlock.gif        
//        'Page Break function).
InBlock.gif        
//        Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef lpvDest As String, ByRef lpvSource As Short, ByVal cbCopy As Integer)
InBlock.gif        
//
InBlock.gif
        [DllImport("kernel32.dll")]
InBlock.gif        
private static extern void RtlMoveMemory(ref string lpvDest,ref short lpvSource,int cbCopy);
InBlock.gif        
private FileStream fs;
InBlock.gif        
private BEG_FILE_RECORD m_udtBEG_FILE_MARKER;
InBlock.gif        
private END_FILE_RECORD m_udtEND_FILE_MARKER;
InBlock.gif        
private HPAGE_BREAK_RECORD m_udtHORIZ_PAGE_BREAK;
InBlock.gif
InBlock.gif                                                                                                                                        
InBlock.gif        
//create an array that will hold the rows where a horizontal page break will be inserted just before.
InBlock.gif
        private int[] m_shtHorizPageBreakRows;
InBlock.gif        
private int m_shtNumHorizPageBreaks=1;
InBlock.gif        
InBlock.gif        
private void FilePut(byte[] buf)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fs.Write(buf,
0,buf.Length);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void FilePut(System.ValueType vt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            Type t 
= vt.GetType();
InBlock.gif            
int size = 0;
InBlock.gif
InBlock.gif
//            foreach(FieldInfo fi in t.GetFields())
InBlock.gif
//            {
InBlock.gif
//                size += Marshal.SizeOf(fi.FieldType);
InBlock.gif
//                
InBlock.gif
//                System.Diagnostics.Trace.WriteLine(fi.Name);
InBlock.gif
//            }
InBlock.gif
            
InBlock.gif            size 
= Marshal.SizeOf(vt);
InBlock.gif            IntPtr p 
= Marshal.AllocHGlobal(size);
InBlock.gif            Marshal.StructureToPtr(vt,p,
true);
InBlock.gif            
byte[] buf = new byte[size];
InBlock.gif            Marshal.Copy(p,buf,
0,size);            
InBlock.gif
InBlock.gif            fs.Write(buf,
0,buf.Length);
InBlock.gif            Marshal.FreeHGlobal(p);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void FilePut(System.ValueType vt,int len)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
int size = 0;
InBlock.gif            size 
= len;
InBlock.gif            IntPtr p 
= Marshal.AllocHGlobal(size);
InBlock.gif            Marshal.StructureToPtr(vt,p,
true);
InBlock.gif            
byte[] buf = new byte[size];
InBlock.gif            Marshal.Copy(p,buf,
0,size);            
InBlock.gif
InBlock.gif            fs.Write(buf,
0,buf.Length);
InBlock.gif            Marshal.FreeHGlobal(p);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool PrintGridLines
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    PRINT_GRIDLINES_RECORD GRIDLINES_RECORD;
InBlock.gif        
InBlock.gif                    GRIDLINES_RECORD.opcode 
= 43;
InBlock.gif                    GRIDLINES_RECORD.length 
= 2;
InBlock.gif                    
if(true == value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        GRIDLINES_RECORD.PrintFlag 
= 1;    
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        GRIDLINES_RECORD.PrintFlag 
= 0;    
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    FilePut(GRIDLINES_RECORD);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw ex;
ExpandedSubBlockEnd.gif                }
                                                                                                                                        
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public bool ProtectSpreadsheet
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    PROTECT_SPREADSHEET_RECORD PROTECT_RECORD;
InBlock.gif        
InBlock.gif                                           
InBlock.gif                    PROTECT_RECORD.opcode 
= 18;
InBlock.gif                    PROTECT_RECORD.length 
= 2;
InBlock.gif                    
if(true == value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        PROTECT_RECORD.Protect 
= 1;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        PROTECT_RECORD.Protect 
= 0;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
InBlock.gif                    
if(null == fs)throw new SmartExcelOpeartionFileException();
InBlock.gif                    FilePut(PROTECT_RECORD);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw ex;    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//
InBlock.gif
        public void CreateFile(string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(File.Exists(strFileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    File.SetAttributes(strFileName, FileAttributes.Normal);
InBlock.gif                    File.Delete(strFileName);
ExpandedSubBlockEnd.gif                }

InBlock.gif        
InBlock.gif                fs 
= new FileStream(strFileName,FileMode.CreateNew);
InBlock.gif                FilePut(m_udtBEG_FILE_MARKER);
InBlock.gif        
InBlock.gif                WriteDefaultFormats();
InBlock.gif                
//                    'create the Horizontal Page Break array
ExpandedSubBlockStart.gifContractedSubBlock.gif
                m_shtHorizPageBreakRows = new int[1]dot.gif{0};
InBlock.gif                m_shtNumHorizPageBreaks 
= 0;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//
InBlock.gif
        public void CloseFile()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
short x;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(null != fs)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//'write the horizontal page breaks if necessary
InBlock.gif
                    int lLoop1,lLoop2,lTemp;
InBlock.gif
InBlock.gif                    
if(m_shtNumHorizPageBreaks > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//                                               'the Horizontal Page Break array must be in sorted order.
InBlock.gif                        
//                'Use a simple Bubble sort because the size of this array would
InBlock.gif                        
//                'be pretty small most of the time. A QuickSort would probably
InBlock.gif                        
//                'be overkill.
InBlock.gif
                        for(lLoop1=m_shtHorizPageBreakRows.GetUpperBound(0);lLoop1>=m_shtHorizPageBreakRows.GetLowerBound(0);lLoop1--)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
for(lLoop2=m_shtHorizPageBreakRows.GetLowerBound(0)+1;lLoop2<=lLoop1;lLoop2++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
if(m_shtHorizPageBreakRows[lLoop2 - 1> m_shtHorizPageBreakRows[lLoop2])
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    lTemp 
= m_shtHorizPageBreakRows[lLoop2 - 1];
InBlock.gif                                    m_shtHorizPageBreakRows[lLoop2 
- 1= m_shtHorizPageBreakRows[lLoop2];
InBlock.gif                                    m_shtHorizPageBreakRows[lLoop2] 
= (short)lTemp;
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
//'write the Horizontal Page Break Record
InBlock.gif
                        m_udtHORIZ_PAGE_BREAK.opcode = 27;
InBlock.gif                        m_udtHORIZ_PAGE_BREAK.length 
= (short)(2 + (m_shtNumHorizPageBreaks * 2));
InBlock.gif                        m_udtHORIZ_PAGE_BREAK.NumPageBreaks 
= (short)m_shtNumHorizPageBreaks;
InBlock.gif                                     
InBlock.gif                        FilePut(m_udtHORIZ_PAGE_BREAK);
InBlock.gif        
InBlock.gif                        
//                                             'now write the actual page break values
InBlock.gif                        
//                'the MKI$ function is standard in other versions of BASIC but
InBlock.gif                        
//                'VisualBasic does not have it. A KnowledgeBase article explains
InBlock.gif                        
//                'how to recreate it (albeit using 16-bit API, I switched it
InBlock.gif                        
//                'to 32-bit).
InBlock.gif
                        for(x = 1;x<=m_shtHorizPageBreakRows.GetUpperBound(0);x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            FilePut(System.Text.Encoding.Default.GetBytes(MKI((
short)(m_shtHorizPageBreakRows[x]))));
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    FilePut(m_udtEND_FILE_MARKER);
InBlock.gif                    fs.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void Init()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//                                                                         'Set up default values for records
InBlock.gif            
//        'These should be the values that are the same for every record of these types
InBlock.gif        
InBlock.gif            
// beginning of file
InBlock.gif
            m_udtBEG_FILE_MARKER.opcode = 9;
InBlock.gif            m_udtBEG_FILE_MARKER.length 
= 4;
InBlock.gif            m_udtBEG_FILE_MARKER.version 
= 2;
InBlock.gif            m_udtBEG_FILE_MARKER.ftype 
= 10;
InBlock.gif
InBlock.gif            
// end of file marker
InBlock.gif
            m_udtEND_FILE_MARKER.opcode = 10;            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SmartExcel()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Init();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif        
public void InsertHorizPageBreak(int lrow)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int row;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//    'the row and column values are written to the excel file as
InBlock.gif                
//    'unsigned integers. Therefore, must convert the longs to integer.
InBlock.gif
                if(lrow>32767 || lrow<0)row = 0;
InBlock.gif                
else row = lrow-1;
InBlock.gif                m_shtNumHorizPageBreaks 
= m_shtNumHorizPageBreaks + 1;
InBlock.gif                m_shtHorizPageBreakRows[m_shtNumHorizPageBreaks] 
= row;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
        
InBlock.gif        
InBlock.gif        
public void WriteValue(ValueTypes ValueType , CellFont CellFontUsed, CellAlignment Alignment, CellHiddenLocked HiddenLocked , int lrow, int lcol, object Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WriteValue(ValueType , CellFontUsed, Alignment, HiddenLocked , lrow, lcol, Value,
0);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void WriteValue(ValueTypes ValueType , CellFont CellFontUsed, CellAlignment Alignment, CellHiddenLocked HiddenLocked , int lrow, int lcol, object Value,int CellFormat)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int l;
InBlock.gif            
string st;
InBlock.gif            
short col,row;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//                            'the row and column values are written to the excel file as
InBlock.gif                
//                'unsigned integers. Therefore, must convert the longs to integer.
InBlock.gif
                tInteger INTEGER_RECORD;
InBlock.gif                tNumber NUMBER_RECORD;
InBlock.gif                
byte b;
InBlock.gif                tText TEXT_RECORD;
InBlock.gif                
if(lrow>32767 || lrow<0)row = 0;
InBlock.gif                
else row = (short)(lrow-1);
InBlock.gif                
if(lcol > 32767 || lcol<0)col = 0;else col = (short)(lcol - 1);
InBlock.gif                
switch(ValueType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case ValueTypes.Integer:
InBlock.gif                        INTEGER_RECORD.opcode 
= 2;
InBlock.gif                        INTEGER_RECORD.length 
= 9;
InBlock.gif                        INTEGER_RECORD.row 
= row;
InBlock.gif                        INTEGER_RECORD.col 
= col;
InBlock.gif                        INTEGER_RECORD.rgbAttr1 
= (byte)(HiddenLocked);
InBlock.gif                        INTEGER_RECORD.rgbAttr2 
= (byte)(CellFontUsed + CellFormat);
InBlock.gif                        INTEGER_RECORD.rgbAttr3 
= (byte)(Alignment);
InBlock.gif                        INTEGER_RECORD.intValue 
= (short)(Value);
InBlock.gif                        FilePut(INTEGER_RECORD);
InBlock.gif                        
break;
InBlock.gif                    
case ValueTypes.Number:
InBlock.gif                        NUMBER_RECORD.opcode 
= 3;
InBlock.gif                        NUMBER_RECORD.length 
= 15;
InBlock.gif                        NUMBER_RECORD.row 
= row;
InBlock.gif                        NUMBER_RECORD.col 
= col;
InBlock.gif                        NUMBER_RECORD.rgbAttr1 
= (byte)(HiddenLocked);
InBlock.gif                        NUMBER_RECORD.rgbAttr2 
= (byte)(CellFontUsed + CellFormat);
InBlock.gif                        NUMBER_RECORD.rgbAttr3 
= (byte)(Alignment);
InBlock.gif                        NUMBER_RECORD.NumberValue 
= (double)(Value);
InBlock.gif                        FilePut(NUMBER_RECORD);
InBlock.gif                        
break;
InBlock.gif                    
case ValueTypes.Text:
InBlock.gif                        st 
= Convert.ToString(Value);
InBlock.gif                        l 
= GetLength(st);// 'LenB(StrConv(st, vbFromUnicode)) 'Len(st$)
InBlock.gif
                    
InBlock.gif                        TEXT_RECORD.opcode 
= 4;
InBlock.gif                        TEXT_RECORD.length 
= 10;
InBlock.gif                        
//'Length of the text portion of the record
InBlock.gif
                        TEXT_RECORD.TextLength = (byte)l;
InBlock.gif                        
//                          'Total length of the record
InBlock.gif
                        TEXT_RECORD.length = (byte)(8 + l);
InBlock.gif                        TEXT_RECORD.row 
= row;
InBlock.gif                        TEXT_RECORD.col 
= col;
InBlock.gif                        TEXT_RECORD.rgbAttr1 
= (byte)(HiddenLocked);
InBlock.gif                        TEXT_RECORD.rgbAttr2 
= (byte)(CellFontUsed + CellFormat);
InBlock.gif                        TEXT_RECORD.rgbAttr3 
= (byte)(Alignment);
InBlock.gif                        
//                        'Put record header
InBlock.gif
                        FilePut(TEXT_RECORD);
InBlock.gif                        
//                'Then the actual string data
InBlock.gif
                        FilePut(System.Text.Encoding.Default.GetBytes(st));
InBlock.gif                        
break;
InBlock.gif                    
default:break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void SetMargin(MarginTypes Margin , double MarginValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//                      'write the spreadsheet's layout information (in inches)
InBlock.gif
                MARGIN_RECORD_LAYOUT MarginRecord;
InBlock.gif                MarginRecord.opcode 
= (short)Margin;
InBlock.gif                MarginRecord.length 
= 8;
InBlock.gif                MarginRecord.MarginValue 
= MarginValue;// 'in inches
InBlock.gif
                FilePut(MarginRecord);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void SetColumnWidth(int FirstColumn, int LastColumn, short WidthValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                COLWIDTH_RECORD COLWIDTH;
InBlock.gif                            
InBlock.gif                COLWIDTH.opcode 
= 36;
InBlock.gif                COLWIDTH.length 
= 4;
InBlock.gif                COLWIDTH.col1 
= (byte)(FirstColumn - 1);
InBlock.gif                COLWIDTH.col2 
= (byte)(LastColumn - 1);
InBlock.gif                COLWIDTH.ColumnWidth 
= (short)(WidthValue * 256);// 'values are specified as 1/256 of a character
InBlock.gif
                FilePut(COLWIDTH);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void SetFont(string FontName, short FontHeight, FontFormatting FontFormat)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int l;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//     'you can set up to 4 fonts in the spreadsheet file. When writing a value such
InBlock.gif                
//    'as a Text or Number you can specify one of the 4 fonts (numbered 0 to 3)
InBlock.gif
                FONT_RECORD FONTNAME_RECORD;
InBlock.gif                l 
= GetLength(FontName);// 'LenB(StrConv(FontName, vbFromUnicode)) 'Len(FontName)
InBlock.gif
                FONTNAME_RECORD.opcode = 49;
InBlock.gif                FONTNAME_RECORD.length 
= (short)(5 + l);
InBlock.gif                FONTNAME_RECORD.FontHeight 
= (short)(FontHeight * 20);
InBlock.gif                FONTNAME_RECORD.FontAttributes1 
= (byte)FontFormat;// 'bold/underline etcdot.gif
InBlock.gif
                FONTNAME_RECORD.FontAttributes2 = (byte)0;// 'reserved-always zero!!
InBlock.gif
                FONTNAME_RECORD.FontNameLength = (byte)l;//'CByte(Len(FontName))
InBlock.gif
                FilePut(FONTNAME_RECORD);
InBlock.gif                
//                        'Then the actual font name data
InBlock.gif
                FilePut(System.Text.Encoding.Default.GetBytes(FontName));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void SetHeader(string HeaderText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int l;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HEADER_FOOTER_RECORD HEADER_RECORD;
InBlock.gif                l 
= GetLength(HeaderText);//   'LenB(StrConv(HeaderText, vbFromUnicode)) 'Len(HeaderText)
InBlock.gif
                HEADER_RECORD.opcode = 20;
InBlock.gif                HEADER_RECORD.length 
= (short)(1 + l);
InBlock.gif                HEADER_RECORD.TextLength 
= (byte)l;// 'CByte(Len(HeaderText))
InBlock.gif
                FilePut(HEADER_RECORD);
InBlock.gif                
//                        'Then the actual Header text
InBlock.gif
                FilePut(System.Text.Encoding.Default.GetBytes(HeaderText));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void SetFooter(string FooterText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int l;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HEADER_FOOTER_RECORD FOOTER_RECORD;
InBlock.gif                l 
= GetLength(FooterText);// 'LenB(StrConv(FooterText, vbFromUnicode)) 'Len(FooterText)
InBlock.gif
                FOOTER_RECORD.opcode = 21;
InBlock.gif                FOOTER_RECORD.length 
= (short)(1 + l);
InBlock.gif                FOOTER_RECORD.TextLength 
= (byte)l;
InBlock.gif                FilePut(FOOTER_RECORD);
InBlock.gif                
//                    'Then the actual Header text
InBlock.gif
                FilePut(System.Text.Encoding.Default.GetBytes(FooterText));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void SetFilePassword(string PasswordText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int l;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PASSWORD_RECORD FILE_PASSWORD_RECORD;
InBlock.gif                l 
= GetLength(PasswordText);// 'LenB(StrConv(PasswordText, vbFromUnicode)) 'Len(PasswordText)
InBlock.gif
                FILE_PASSWORD_RECORD.opcode = 47;
InBlock.gif                FILE_PASSWORD_RECORD.length 
= (short)l;
InBlock.gif                FilePut(FILE_PASSWORD_RECORD);
InBlock.gif                
//          'Then the actual Password text
InBlock.gif
                FilePut(System.Text.Encoding.Default.GetBytes(PasswordText));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void WriteDefaultFormats()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FORMAT_COUNT_RECORD cFORMAT_COUNT_RECORD;
InBlock.gif            FORMAT_RECORD cFORMAT_RECORD;
InBlock.gif            
int lIndex;
InBlock.gif            
int l;
InBlock.gif            
string q = """;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] aFormat = new string[]dot.gif{
InBlock.gif                                               
"General",
InBlock.gif                                               
"0",
InBlock.gif                                               
"0.00",
InBlock.gif                                               
"#,##0",
InBlock.gif                                               
"#,##0.00",
InBlock.gif                                               
"#,##0\ "+q+"$"+q+";\-#,##0\ "+q+"$"+q,
InBlock.gif                                               
"#,##0\ "+q+"$"+q+";[Red]\-#,##0\ "+q+"$"+q,
InBlock.gif                                               
"#,##0.00\ "+q+"$"+q+";\-#,##0.00\ "+q+"$"+q,
InBlock.gif                                               
"#,##0.00\ "+q+"$"+q+";[Red]\-#,##0.00\ "+q+"$"+q,
InBlock.gif                                               
"0%",
InBlock.gif                                               
"0.00%",
InBlock.gif                                               
"0.00E+00",
InBlock.gif                                               
"dd/mm/yy",
InBlock.gif                                               
"dd/\ mmm\ yy",
InBlock.gif                                               
"dd/\ mmm",
InBlock.gif                                               
"mmm\ yy",
InBlock.gif                                               
"h:mm\ AM/PM",
InBlock.gif                                               
"h:mm:ss\ AM/PM",
InBlock.gif                                               
"hh:mm",
InBlock.gif                                               
"hh:mm:ss",
InBlock.gif                                               
"dd/mm/yy\ hh:mm",
InBlock.gif                                               
"##0.0E+0",
InBlock.gif                                               
"mm:ss",
ExpandedSubBlockEnd.gif                                               
"@"}
;
InBlock.gif        
InBlock.gif                
InBlock.gif            cFORMAT_COUNT_RECORD.opcode 
= 0x1f;
InBlock.gif            cFORMAT_COUNT_RECORD.length 
= 0x02;
InBlock.gif            cFORMAT_COUNT_RECORD.Count 
= (short)(aFormat.GetUpperBound(0));
InBlock.gif            FilePut(cFORMAT_COUNT_RECORD);
InBlock.gif        
InBlock.gif            
byte b;
InBlock.gif            
int a;
InBlock.gif            
for(lIndex = aFormat.GetLowerBound(0);lIndex<=aFormat.GetUpperBound(0);lIndex++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                l 
= aFormat[lIndex].Length;
InBlock.gif                cFORMAT_RECORD.opcode 
= 0x1e;
InBlock.gif                cFORMAT_RECORD.length 
= (short)(l+1);
InBlock.gif                cFORMAT_RECORD.FormatLength 
= (byte)(l);
InBlock.gif                FilePut(cFORMAT_RECORD);
InBlock.gif                
//                Then the actual format
InBlock.gif                
// 从1开始还是从0开始?!
InBlock.gif
                for(a=0;a<l;a++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    b 
= (byte)(aFormat[lIndex].Substring(a,1).ToCharArray(0,1)[0]);
ExpandedSubBlockStart.gifContractedSubBlock.gif                    FilePut(
new byte[]dot.gif{b});
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private string MKI(short x)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string temp;
InBlock.gif            
//'used for writing integer array values to the disk file
InBlock.gif
            temp = "  ";
InBlock.gif            RtlMoveMemory(
ref temp, ref x, 2);
InBlock.gif            
return temp;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private int GetLength(string strText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return System.Text.Encoding.Default.GetBytes(strText).Length;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void SetDefaultRowHeight(int HeightValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//              'Height is defined in units of 1/20th of a point. Therefore, a 10-point font
InBlock.gif                
//                'would be 200 (i.e. 200/20 = 10). This function takes a HeightValue such as
InBlock.gif                
//                '14 point and converts it the correct size before writing it to the file.
InBlock.gif
        
InBlock.gif                DEF_ROWHEIGHT_RECORD DEFHEIGHT;
InBlock.gif                DEFHEIGHT.opcode 
= 37;
InBlock.gif                DEFHEIGHT.length 
= 2;
InBlock.gif                DEFHEIGHT.RowHeight 
= HeightValue * 20;//  'convert points to 1/20ths of point
InBlock.gif
                FilePut(DEFHEIGHT);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void SetRowHeight(int Row,short HeightValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int o_intRow;
InBlock.gif            
//               'the row and column values are written to the excel file as
InBlock.gif            
//                'unsigned integers. Therefore, must convert the longs to integer.
InBlock.gif
            if(Row > 32767)throw new Exception("行号不能大于32767!");
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                o_intRow 
= Row;
InBlock.gif                
//                if(Row > 32767){
InBlock.gif                
//                                   o_intRow = CInt(Row - 65536)
InBlock.gif                
//                                   Else
InBlock.gif                
//                                       o_intRow = CInt(Row) - 1    'rows/cols in Excel binary file are zero based
InBlock.gif                
//                                                                   }
InBlock.gif
InBlock.gif                
//                    'Height is defined in units of 1/20th of a point. Therefore, a 10-point font
InBlock.gif                
//                'would be 200 (i.e. 200/20 = 10). This function takes a HeightValue such as
InBlock.gif                
//                '14 point and converts it the correct size before writing it to the file.
InBlock.gif
        
InBlock.gif                ROW_HEIGHT_RECORD ROWHEIGHTREC;
InBlock.gif                ROWHEIGHTREC.opcode 
= 8;
InBlock.gif                ROWHEIGHTREC.length 
= 16;
InBlock.gif                ROWHEIGHTREC.RowNumber 
= o_intRow;
InBlock.gif                ROWHEIGHTREC.FirstColumn 
= 0;
InBlock.gif                ROWHEIGHTREC.LastColumn 
= 256;
InBlock.gif                ROWHEIGHTREC.RowHeight 
= HeightValue * 20;// 'convert points to 1/20ths of point
InBlock.gif
                ROWHEIGHTREC.internals = 0;
InBlock.gif                ROWHEIGHTREC.DefaultAttributes 
= 0;
InBlock.gif                ROWHEIGHTREC.FileOffset 
= 0;
InBlock.gif                ROWHEIGHTREC.rgbAttr1 
= 0;
InBlock.gif                ROWHEIGHTREC.rgbAttr2 
= 0;
InBlock.gif                ROWHEIGHTREC.rgbAttr3 
= 0;
InBlock.gif                FilePut(ROWHEIGHTREC);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/juqiang/archive/2004/07/08/22255.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值