Skip to content

Commit

Permalink
support multiple rows in sheet header
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Qu committed Apr 18, 2015
1 parent 144b70a commit 6c898a5
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 42 deletions.
12 changes: 6 additions & 6 deletions Toxy.Test/CsvParserTest.cs
Expand Up @@ -15,19 +15,19 @@ public void TestParseToxySpreadsheet()
string path = TestDataSample.GetFilePath("countrylist.csv", null);

ParserContext context=new ParserContext(path);
context.Properties.Add("HasHeader", "1");
context.Properties.Add("ExtractHeader", "1");
ISpreadsheetParser parser = (ISpreadsheetParser)ParserFactory.CreateSpreadsheet(context);
ToxySpreadsheet ss= parser.Parse();
Assert.AreEqual(1, ss.Tables.Count);
Assert.AreEqual(14, ss.Tables[0].ColumnHeaders.Cells.Count);
Assert.AreEqual("Sort Order", ss.Tables[0].ColumnHeaders.Cells[0].Value);
Assert.AreEqual("Sub Type", ss.Tables[0].ColumnHeaders.Cells[4].Value);
Assert.AreEqual(14, ss.Tables[0].HeaderRows[0].Cells.Count);
Assert.AreEqual("Sort Order", ss.Tables[0].HeaderRows[0].Cells[0].Value);
Assert.AreEqual("Sub Type", ss.Tables[0].HeaderRows[0].Cells[4].Value);
Assert.AreEqual(272, ss.Tables[0].Rows.Count);
Assert.AreEqual(12, ss.Tables[0].Rows[12].RowIndex);
Assert.AreEqual(13, ss.Tables[0].Rows[12].RowIndex);
Assert.AreEqual("Kingdom of Bahrain", ss.Tables[0].Rows[12].Cells[2].ToString());
Assert.AreEqual(".bo", ss.Tables[0].Rows[20].Cells[13].ToString());

Assert.AreEqual(271, ss.Tables[0].LastRowIndex);
Assert.AreEqual(272, ss.Tables[0].LastRowIndex);
}
[Test]
public void TestParseIndexOutOfRange()
Expand Down
2 changes: 1 addition & 1 deletion Toxy.Test/ExcelParserBaseTest.cs
Expand Up @@ -32,7 +32,7 @@ public void BaseTestWithoutHeader(string filename)
Assert.IsNull(ss.Tables[0].PageHeader);

ToxySpreadsheet ss2 = parser.Parse();
Assert.AreEqual(0, ss2.Tables[0].ColumnHeaders.Cells.Count);
Assert.AreEqual(0, ss2.Tables[0].HeaderRows.Count);
Assert.AreEqual(9, ss2.Tables[0].Rows.Count);
}
public void BaseTestHeader(string filename)
Expand Down
36 changes: 19 additions & 17 deletions Toxy.Test/TestToxySpreadsheet.cs
Expand Up @@ -40,24 +40,25 @@ public void TestToxyTableToDataTable()
#region create ToxyTable
ToxyTable ttable = new ToxyTable();
ttable.Name = "Test1";
ttable.ColumnHeaders.Cells.Add(new ToxyCell(0, "C1"));
ttable.ColumnHeaders.Cells.Add(new ToxyCell(1, "C2"));
ttable.ColumnHeaders.Cells.Add(new ToxyCell(2, "C3"));
ttable.ColumnHeaders.Cells.Add(new ToxyCell(3, "C4"));
ToxyRow trow1=new ToxyRow(0);
ttable.HeaderRows.Add(new ToxyRow(0));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(0, "C1"));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(1, "C2"));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(2, "C3"));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(3, "C4"));
ToxyRow trow1=new ToxyRow(1);
trow1.Cells.Add(new ToxyCell(0,"1"));
trow1.Cells.Add(new ToxyCell(1,"2"));
trow1.Cells.Add(new ToxyCell(2,"3"));
ttable.Rows.Add(trow1);

ToxyRow trow2 = new ToxyRow(1);
ToxyRow trow2 = new ToxyRow(2);
trow2.Cells.Add(new ToxyCell(0, "4"));
trow2.Cells.Add(new ToxyCell(1, "5"));
trow2.Cells.Add(new ToxyCell(3, "6"));
trow2.LastCellIndex = 3;
ttable.Rows.Add(trow2);

ToxyRow trow3 = new ToxyRow(2);
ToxyRow trow3 = new ToxyRow(3);
trow3.LastCellIndex = 3;
trow3.Cells.Add(new ToxyCell(1, "7"));
trow3.Cells.Add(new ToxyCell(2, "8"));
Expand Down Expand Up @@ -93,27 +94,28 @@ public void TestToxyTableToDataTable_withEmptyColumnHeader()
#region create ToxyTable
ToxyTable ttable = new ToxyTable();
ttable.Name = "Test1";
ttable.ColumnHeaders.Cells.Add(new ToxyCell(0, "C1"));
ttable.ColumnHeaders.Cells.Add(new ToxyCell(1, null));
ttable.ColumnHeaders.Cells.Add(new ToxyCell(2, "C2"));
ttable.ColumnHeaders.Cells.Add(new ToxyCell(3, null));
ttable.ColumnHeaders.Cells.Add(new ToxyCell(4, "C4"));
ToxyRow trow1 = new ToxyRow(0);
ttable.HeaderRows.Add(new ToxyRow(0));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(0, "C1"));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(1, null));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(2, "C2"));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(3, null));
ttable.HeaderRows[0].Cells.Add(new ToxyCell(4, "C4"));
ToxyRow trow1 = new ToxyRow(1);
trow1.Cells.Add(new ToxyCell(0, "1"));
trow1.Cells.Add(new ToxyCell(1, "2"));
trow1.Cells.Add(new ToxyCell(4, "3"));
trow1.Cells.Add(new ToxyCell(5, "4"));
trow1.LastCellIndex = 5;
ttable.Rows.Add(trow1);

ToxyRow trow2 = new ToxyRow(1);
trow2.LastCellIndex = 3;

ToxyRow trow2 = new ToxyRow(2);
trow2.Cells.Add(new ToxyCell(0, "5"));
trow2.Cells.Add(new ToxyCell(1, "6"));
trow2.Cells.Add(new ToxyCell(3, "7"));
trow2.LastCellIndex = 3;
ttable.Rows.Add(trow2);

ttable.LastColumnIndex = 5;
ttable.LastColumnIndex = 3;
#endregion

DataTable dt = ttable.ToDataTable();
Expand Down
20 changes: 11 additions & 9 deletions ToxyFramework/Entities/ToxySpreadsheet/ToxyTable.cs
Expand Up @@ -11,14 +11,14 @@ public class ToxyTable
public ToxyTable()
{
this.Name = string.Empty;
this.ColumnHeaders = new ToxyRow(0);
this.HeaderRows = new List<ToxyRow>();
this.Rows = new List<ToxyRow>();
this.LastColumnIndex = -1;
this.MergeCells = new List<MergeCellRange>();
}
public bool HasHeader
{
get { return this.ColumnHeaders.Cells.Count > 0; }
get { return this.HeaderRows.Count> 0; }
}

public List<MergeCellRange> MergeCells { get; set; }
Expand All @@ -28,7 +28,7 @@ public bool HasHeader
public int LastRowIndex { get; set; }
public int LastColumnIndex { get; set; }

public ToxyRow ColumnHeaders { get; set; }
public List<ToxyRow> HeaderRows { get; set; }

public List<ToxyRow> Rows { get; set; }

Expand All @@ -52,9 +52,11 @@ public DataTable ToDataTable()

int lastCol = 0;
DataTable dt = new DataTable(this.Name);

int rowIndex = 0;
if (HasHeader)
{
foreach (var header in ColumnHeaders.Cells)
foreach (var header in HeaderRows[0].Cells)
{
var col = new DataColumn(header.Value);
dt.Columns.Add(col);
Expand All @@ -65,19 +67,19 @@ public DataTable ToDataTable()
dt.Columns.Add(string.Empty);
lastCol++;
}
rowIndex++;
}
int lastRow = 0;
foreach (var row in this.Rows)
{
DataRow drow = null;
if (lastRow < row.RowIndex)
if (rowIndex < row.RowIndex)
{
drow = dt.NewRow();
while (lastRow < row.RowIndex)
while (rowIndex < row.RowIndex)
{
dt.Rows.Add(drow);
drow = dt.NewRow();
lastRow++;
rowIndex++;
}
}
else
Expand All @@ -100,7 +102,7 @@ public DataTable ToDataTable()
drow = dt.NewRow();
}
dt.Rows.Add(drow);
lastRow++;
rowIndex++;
}
return dt;
}
Expand Down
21 changes: 13 additions & 8 deletions ToxyFramework/Parsers/CSVSpreadsheetParser.cs
Expand Up @@ -34,12 +34,12 @@ public ToxySpreadsheet Parse()
if (!File.Exists(Context.Path))
throw new FileNotFoundException("File " + Context.Path + " is not found");

bool hasHeader=false;
if (Context.Properties.ContainsKey("HasHeader"))
bool extractHeader=false;
if (Context.Properties.ContainsKey("ExtractHeader"))
{
string sHasHeader = Context.Properties["HasHeader"].ToLower();
string sHasHeader = Context.Properties["ExtractHeader"].ToLower();
if (sHasHeader == "1" || sHasHeader == "on" || sHasHeader == "true")
hasHeader = true;
extractHeader = true;
}
char delimiter =',';
if (Context.Properties.ContainsKey("delimiter"))
Expand All @@ -61,18 +61,23 @@ public ToxySpreadsheet Parse()
{
sr = new StreamReader(Context.Path, true);
}
CsvReader reader=new CsvReader(sr, hasHeader,delimiter);
CsvReader reader=new CsvReader(sr, extractHeader,delimiter);
string[] headers = reader.GetFieldHeaders();
ToxySpreadsheet ss = new ToxySpreadsheet();
ToxyTable t1 = new ToxyTable();
ss.Tables.Add(t1);

int i = 0;
if (headers.Length > 0)
{
t1.HeaderRows.Add(new ToxyRow(i));
i++;
}
for (int j = 0; j < headers.Length;j++ )
{
t1.ColumnHeaders.Cells.Add(new ToxyCell(j, headers[j]));
t1.LastColumnIndex = t1.ColumnHeaders.Cells.Count-1;
t1.HeaderRows[0].Cells.Add(new ToxyCell(j, headers[j]));
t1.LastColumnIndex = t1.HeaderRows[0].Cells.Count-1;
}
int i=0;
while(reader.ReadNextRecord())
{
ToxyRow tr=new ToxyRow(i);
Expand Down
3 changes: 2 additions & 1 deletion ToxyFramework/Parsers/ExcelSpreadsheetParser.cs
Expand Up @@ -101,7 +101,8 @@ ToxyTable Parse(IWorkbook workbook, int sheetIndex, bool extractHeader, bool ext
{
if (hasHeader && firstRow)
{
table.ColumnHeaders.Cells.Add(new ToxyCell(cell.ColumnIndex, cell.ToString()));
table.HeaderRows.Add(new ToxyRow(row.RowNum));
table.HeaderRows[0].Cells.Add(new ToxyCell(cell.ColumnIndex, cell.ToString()));
}
else
{
Expand Down
23 changes: 23 additions & 0 deletions release.bat
@@ -0,0 +1,23 @@
@echo off

set fdir=%WINDIR%\Microsoft.NET\Framework64

if not exist %fdir% (
set fdir=%WINDIR%\Microsoft.NET\Framework
)

set msbuild=%fdir%\v4.0.30319\msbuild.exe

%msbuild% ToxyFramework\ToxyFramework_dotnet45.csproj /p:Configuration=Release /t:Rebuild /p:OutputPath=..\Release\Net45

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G"

%msbuild% ToxyFramework\ToxyFramework_dotnet4.csproj /p:Configuration=Release /t:Rebuild /p:OutputPath=..\Release\Net40

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G"

%msbuild% ToxyFramework\ToxyFramework.csproj /p:Configuration=Release /t:Rebuild /p:OutputPath=..\Release\Net35

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G"

pause

0 comments on commit 6c898a5

Please sign in to comment.