Transferred from
Http://hi.baidu.com/david_jdai/item/d3bf00262cd904140975085d
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 ";
+}
----------------- For bloger -------------------
Today, we found that koogra has been updated to version 3.1.1. Only dll files are supported. However, you can decompile the source code with reflector.
Project address: koogra
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 ";
+}
----------------- For bloger -------------------
Today, we found that koogra has been updated to version 3.1.1. Only dll files are supported. However, you can decompile the source code with reflector.
Project address: koogra