Good day,
Maybe I'm wrong read, but I never found the opportunity, how to name columns by the column names in the SQL table.
SQL table I used as a source of data and need to exit in the first row contains the column names.
The result should look like the attached image.
Thank you for your help and example source code.
public
override
List<Worksheet> GetSheets()
{
var result =
new
List<Worksheet>();
DataTable data = GetData();
var sheet =
new
Worksheet();
sheet.Name =
"Srpen 2016"
;
int
rowIndex = 1;
foreach
(DataRow dataRow
in
data.Rows)
{
var row =
new
Row() { Index = rowIndex++ };
int
columnIndex = 0;
foreach
(DataColumn dataColumn
in
data.Columns)
{
if
(dataColumn.ColumnName ==
"DochazkaID"
)
continue
;
string
cellValue = dataRow[dataColumn.ColumnName].ToString();
var cell =
new
Cell() { Index = columnIndex++, Value = cellValue };
row.AddCell(cell);
}
sheet.AddRow(row);
}
result.Add(sheet);
return
result;
}
public
DataTable GetData()
{
using
(SqlConnection conn =
new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[
"DefaultConnection"
].ConnectionString))
{
conn.Open();
string
query =
"SELECT Dochazka.DochazkaID,Zamestnanci.Prijmeni,Zamestnanci.Jmeno,Zakazky.Zkratka,Dochazka.MesicID,Dochazka.Den1,Dochazka.Den2,Dochazka.Den3,Dochazka.Den4,Dochazka.Den5,Dochazka.Den6,Dochazka.Den7,Dochazka.Den8,Dochazka.Den9,Dochazka.Den10,Dochazka.Den11,Dochazka.Den12,Dochazka.Den13,Dochazka.Den14,Dochazka.Den15,Dochazka.Den16,Dochazka.Den17,Dochazka.Den18,Dochazka.Den19,Dochazka.Den20,Dochazka.Den21,Dochazka.Den22,Dochazka.Den23,Dochazka.Den24,Dochazka.Den25,Dochazka.Den26,Dochazka.Den27,Dochazka.Den28,Dochazka.Den29,Dochazka.Den30,Dochazka.Den31 FROM dbo.Dochazka INNER JOIN dbo.Zamestnanci ON Dochazka.ZamestnanecID = Zamestnanci.ZamestnanecID INNER JOIN dbo.Zakazky ON Dochazka.ZakazkaID = Zakazky.ZakazkaID WHERE Dochazka.MesicID = DATEPART(MONTH, GETDATE()) ORDER BY Zamestnanci.Prijmeni"
;
SqlCommand cmd =
new
SqlCommand(query, conn);
DataTable dt =
new
DataTable();
dt.Load(cmd.ExecuteReader());
return
dt;
}
}