Koogra is an open-source Excel reader on the. NET platform. It can be downloaded from the open-source community. You can use it to read Excel files without office. Although this program has stopped updating, it is still very useful. The following describes how to use it.
Download the source code of the program, compile and generate net. SourceForge. koogra. dll. Reference this DLL in the project, using net. SourceForge. koogra. Excel;
Workbook WB = new Workbook (PATH); path is the physical path of the file, which can create an Excel file object
Worksheet xsheet = xbook. Sheets [0]; reference workbook Worksheet
Xbook. Sheets. getbyname (string) can also be used to obtain reference to the workbook worksheet.
Xbook. Sheets. Rows [I] References to excel rows
Xbook. Sheets. Rows [I]. cells [I] References to cells
The row number of the first line of xsheet. Rows. firstrow, starting from 0.
The row number of the end row of xsheet. Rows. lastrow. For empty rows in the middle, you can use xsheet. Rows [I] = NULL to judge
Cells also have the firstcol and lastcol attributes. If cells are null, cells. value cannot be used.
The following is an example:
/// <Summary>
/// This method just exercises the Excel Workbook data.
/// </Summary>
/// <Param name = "path"> the path to the workbook. </param>
Private workbook dumpworkbooktoconsole (string path)
{
// Print the path
Console. writeline (PATH );
// Construct our workbook
Workbook WB = new Workbook (PATH );
// Dump the worksheet data
Foreach (worksheet ws in WB. Sheets)
{
Console. Write ("sheet is ");
Console. Write (WS. Name );
Console. Write ("First row is :");
Console. Write (WS. Rows. firstrow );
Console. Write ("Last Row is :");
Console. writeline (WS. Rows. lastrow );
// Dump cell data
For (INT r = ws. Rows. firstrow; r <= ws. Rows. lastrow; ++ R)
{
Row row = ws. Rows [(ushort) R];
If (row! = NULL)
{
Console. Write ("row :");
Console. Write (R );
Console. Write ("first Col :");
Console. Write (row. cells. firstcol );
Console. Write ("Last Col :");
Console. writeline (row. cells. lastcol );
For (INT c = row. cells. firstcol; C <= row. cells. lastcol; ++ C)
{
Cell cell = row. cells [(byte) C];
Console. Write ("Col :");
Console. Write (C );
If (cell! = NULL)
{
Console. Write ("value :");
Console. Write (cell. value );
Console. Write ("formatted value :");
Console. writeline (cell. formattedvalue ());
}
Else
Console. writeline ("null ");
}
}
}
}
Return WB;
}
More functions are to be explored ~ It is written in C # And the source code is also available. Let's take a look. In addition, another open-source Dongdong: myxls supports Excel reading and writing, and is still being updated, which is also very good.
Some amendments to koogra:
1. Fixed the bug of garbled Chinese worksheet names
\ Excel \ records \ boundsheetrecord. CS 34 ~ 38 rows
Ushort namelen = reader. readuint16 ();
Stringbuilder NB = new stringbuilder (namelen );
NB. append (new string (reader. readchars (namelen )));
_ Name = Nb. tostring ();
Change
Ushort namelen = (ushort) reader. readbyte ();
Bool compressed = (reader. readbyte () * 0x01) = 0;
If (! Compressed ){
Namelen * = 2;
}
Byte [] charbytes = reader. readbytes (namelen );
If (Compressed ){
// Decompress
Byte [] widebytes = new byte [charbytes. length * 2];
For (INT I = 0; I <charbytes. length; I ++)
Widebytes [2 * I] = charbytes [I];
Charbytes = widebytes;
}
_ Name = new string (encoding. Unicode. getchars (charbytes ));
2. Adjust the default format of the date
Obtain the date data of the Excel worksheet, for example
Cell. formattedvalue () is used to obtain the string "3/5/07"
This is usually not what I want, so I modified the original cell. CS
Public String formattedvalue (){
...
...
// Get the Format String
String formatstring = format. formatvalue;
+ If (formatstring = "m/D/YY "){
+ Formatstring = "yyyy/mm/DD ";
+}