61,655
社区成员




public void Save(string sFilePath)
{
XlsDocument xls = new XlsDocument();
xls.FileName = sFilePath;
int colIndex = 0;
int rowIndex = 1;
Worksheet sheet = xls.Workbook.Worksheets.Add("Sheet1");
Cells cells = sheet.Cells;
// 表头
for (int i = 0; i < _fields.Length; i++)
{
if (_dataSource.Columns.Contains(_fields[i]))
{
colIndex++;
Cell cellHeader = cells.Add(rowIndex, colIndex, _titles[i]);
cellHeader.Font.Bold = true;
cellHeader.Font.ColorIndex = 4;
cellHeader.HorizontalAlignment = HorizontalAlignments.Left;
cellHeader.UseBorder = true;
cellHeader.BottomLineColor = Colors.Black;
cellHeader.BottomLineStyle = 2;
cellHeader.RightLineColor = Colors.Black;
cellHeader.RightLineStyle = 1;
}
}
XF dateStyle = xls.NewXF();
dateStyle.Format = "yyyy-MM-dd";
foreach (DataRow row in _dataSource.Rows)
{
rowIndex++;
colIndex = 0;
foreach (string s in _fields)
{
if (_dataSource.Columns.Contains(s))
{
colIndex++;
string cellValue = row[s].ToString();
cells.Add(rowIndex, colIndex, row[s]);
if (DateTime.TryParse(cellValue, out dtValue))
{
cells.Add(rowIndex, colIndex, dtValue, dateStyle);
}
else
{
cells.Add(rowIndex, colIndex, row[s]);
}
}
}
}
xls.Save(true);
}