日期:2014-05-17  浏览次数:20505 次

excel导出 org.in2bits.MyXls
excel导出是用 org.in2bits.MyXls 做的,有没有人用过呀?
导出来的时间列 不是正常的格式,而是 ”41092“ 这个在哪里设置格式呀?

------解决方案--------------------
C# code

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);
        }