日期:2014-05-18 浏览次数:20496 次
 private void xlsGridview(DataSet bs, string xlsName)
    {
        XlsDocument xls = new XlsDocument();
        int rowIndex = 1;
        int colIndex = 0;
        DataTable table = bs.Tables["singlemoter1"];
        Worksheet sheet = xls.Workbook.Worksheets.AddNamed("sheet");//状态栏标题名称
        Cells cells = sheet.Cells;
        [color=#FF00FF]foreach (DataColumn col in table.Columns)[/color]        {
            colIndex++;
            //sheet.Cells.AddValueCell(1,colIndex,col.ColumnName);//添加XLS标题行
            cells.AddValueCell(1, colIndex, col.ColumnName);
        }
        foreach (DataRow row in table.Rows)
        {
            rowIndex++;
            colIndex = 0;
            foreach (DataColumn col in table.Columns)
            {
                colIndex++;
                //sheet.Cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//将数据添加到xls表格里
                Cell cell = cells.AddValueCell(rowIndex, colIndex,row[col.ColumnName].ToString());//转换为数字型
                //如果你数据库里的数据都是数字的话 最好转换一下,不然导入到Excel里是以字符串形式显示。
                cell.Font.FontFamily = FontFamilies.Roman; //字体
                cell.Font.Bold = true;  //字体为粗体            
            }
        }
        xls.Send();
    }