立博名家

MyXls 使用實例

    private void Export(DateTime startDate, DateTime endDate)
    {
        MergeArea meaTempA;
        int iDay = DiffDay(startDate ,endDate );
        DataSet ds = ReportBLL.Instance.ListDialy(startDate, endDate);
        DataView dv = ds.Tables[0].DefaultView;
        XlsDocument xls = new XlsDocument();
        xls.FileName = "HR.xls";

        //添加文件属性
        xls.SummaryInformation.Author = "Tim Erickson"; //作者
        xls.SummaryInformation.Subject = "A wacky display of Excel file generation";
        xls.DocumentSummaryInformation.Company = "in2bits.org";

        XF cellXF = xls.NewXF();//为xls生成一个XF实例(XF是cell格式对象)
        //cellXF.HorizontalAlignment = HorizontalAlignments.Centered;//设定文字居中
        //cellXF.Font.FontName = "方正舒体";//设定字体
        //cellXF.Font.Height = 20 * 20;//设定字大小(字体大小是以 1/20 point 为单位的)
        cellXF.Font.ColorIndex = 2;
        cellXF.UseBorder = true;//使用边框
        cellXF.BottomLineStyle = 1;//设定边框底线为粗线
        cellXF.BottomLineColor = Colors.Red;//设定颜色为暗红
        cellXF.UseProtection = true;
        cellXF.UseMisc = true;

        XF cellTitle = xls.NewXF();//为xls生成一个XF实例(XF是cell格式对象)
        cellTitle.HorizontalAlignment = HorizontalAlignments.Centered;        
        cellTitle.Font .Bold  = true;
        cellTitle.UseBorder = true;


        //for (int sheetNumber = 1; sheetNumber <= 1; sheetNumber++)
        //{
            string sheetName = startDate.Month.ToString() + "月";
            //int rowMin = 1;
            int rowCount = SumRen ();
           // int colMin = 1;
            //int colCount = sheetNumber + 10;
            //创建5个表格
            Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName);
            Cells cells = sheet.Cells;
            //列印第一行
            cells.Add(1, 1, "深圳员工" +startDate.Month.ToString() + "月考勤").Font.Bold = true;
            cells.Add(1, 2, "");
            cells.Add(1, 3, "");
            cells.Add(1, 4, "");
            cells.Add(1, 5, "");
         
            for (int i = 1; i <= iDay; i++)
            {
                cells.Add(1, 5 + 2 * i - 1, startDate.AddDays(i - 1).ToShortDateString(),cellTitle ); 
                cells.Add(1, 5 + 2 * i, null);
                MergeArea meaA1 = new MergeArea(1, 1, 5 + 2 * i - 1, 5 + 2 * i);//一个合并单元格实例(合并第一行、第三例 到 第二行、第四例)
                sheet.AddMergeArea(meaA1);//填加合并单元格
            }
            //第二行
            for (int i = 1; i <= iDay; i++)
            {
                cells.Add(2, 5 + 2 * i - 1, startDate.AddDays(i - 1).DayOfWeek.ToString(), cellTitle); 
                cells.Add(2, 5 + 2 * i, null);
                MergeArea meaA2 = new MergeArea(2, 2, 5 + 2 * i - 1, 5 + 2 * i);//一个合并单元格实例(合并第一行、第三例 到 第二行、第四例)
                sheet.AddMergeArea(meaA2);//填加合并单元格
            }
            MergeArea meaA = new MergeArea(1, 2, 1, 5);//一个合并单元格实例(合并第一行、第三例 到 第二行、第四例)
            sheet.AddMergeArea(meaA);//填加合并单元格
            //第3行
            cells.Add(3, 1, "序號",cellTitle );
            cells.Add(3, 2, "部門", cellTitle);
            cells.Add(3, 3, "崗位職務", cellTitle);
            cells.Add(3, 4, "中文姓名", cellTitle);
            cells.Add(3, 5, "工作時間", cellTitle);
           
            for (int i = 1; i <= iDay; i++)
            {
                cells.Add(3, 5 + 2 * i - 1, "開工", cellTitle);
                cells.Add(3, 5 + 2 * i, "收工", cellTitle); 
            }
            DataTable dtRen = HR.BLL.Imp_BanCi.Instance.GetMonthUserList(startDate,endDate ).Tables [0];
            DataSet  dsDaySpec = GetDaySpec(startDate, endDate);
            int r = 3;
            //foreach (DataRow everyRen in dtRen.Rows)
            for (int k = 0; k < dtRen.Rows.Count; k++)
            {               
                r++;
                string userNo = dtRen.Rows[k]["UserNo"].ToString();
                DataRow[] drRen = ds.Tables[0].Select("UserNo='" + userNo + "'");

                cells.Add(r, 1, k + 1);
                cells.Add(r, 2, dtRen.Rows[k]["departName"].ToString());
                cells.Add(r, 3, dtRen.Rows[k]["JobsID"].ToString());
                cells.Add(r, 4, dtRen.Rows[k]["empName_Ch"].ToString());

                if (drRen.Length <= 0)
                {
                    r++;                  
                    continue;
                }

                
                cells.Add(r, 5, drRen[0]["workTypeMark"].ToString());
                DataRow[] drEvery = ds.Tables[0].Select( " UserNo='" + userNo + "'");
                for (int i = 0; i < iDay&& i<drEvery .Length ; i++)
                {
                    int kDay = 0;
                    kDay = DiffDay(startDate, DateTime.Parse(drEvery[i]["WorkDate"].ToString())) - 1;
                    //if (i == 0)
                    //{
                    //    for (int t = 0; t < kDay; t++)
                    //    {
                    //        cells.Add(r, 6 + (t * 2), "--").HorizontalAlignment = HorizontalAlignments.Centered;
                    //        cells.Add(r, 6 + (t * 2) + 1, "--").HorizontalAlignment = HorizontalAlignments.Centered;
                    //    }
                    //}
                   
                    if (!string.IsNullOrEmpty(drEvery[i]["time1"].ToString()))
                    {

                        if (drEvery[i]["sFlag1"].ToString() == "1")
                        {
                            cells.Add(r, 6 + (kDay * 2), "" + DateTime.Parse(drEvery[i]["time1"].ToString()).ToShortTimeString()).UseBorder = true;
                        }
                        else
                        {
                            cells.Add(r, 6 + (kDay * 2), "" + DateTime.Parse(drEvery[i]["time1"].ToString()).ToShortTimeString(), cellXF).UseBorder = true;

                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(drEvery[i]["bid"].ToString()))
                        {
                            cells.Add(r, 6 + (kDay * 2), "--").HorizontalAlignment = HorizontalAlignments.Centered;
                        }
                        else
                        {
                            DataRow[] drDaySpec = dsDaySpec.Tables[0].Select(" bid='" + drEvery[i]["bid"].ToString() + "'");
                            if (drDaySpec.Length > 0)
                            {
                                   
                                cells.Add(r, 6 + (kDay * 2), drDaySpec[0]["SpecName"].ToString()).HorizontalAlignment = HorizontalAlignments.Centered ;
                                meaTempA = new MergeArea(r, r, 6 + (kDay * 2), 6 + (kDay * 2) + 1);//一个合并单元格实例(合并第一行、第三例 到 第二行、第四例)
                                sheet.AddMergeArea(meaTempA);//填加合并单元格
                            }                           
                        } 
                    }

                    if (!string.IsNullOrEmpty(drEvery[i]["time4"].ToString()))
                    {
                        if (drEvery[i]["sFlag4"].ToString() == "1")
                        {
                            cells.Add(r, 6 + (kDay * 2 + 1), "" + DateTime.Parse(drEvery[i]["time4"].ToString()).ToShortTimeString()).UseBorder = true;
                        }
                        else
                        {
                            cells.Add(r, 6 + (kDay * 2 + 1), "" + DateTime.Parse(drEvery[i]["time4"].ToString()).ToShortTimeString(), cellXF).TopLineColor = Colors.Red;
                        }

                    }

                }
                 

            }
        //}
            

            
           
         
           
        

            /*  */

            
        xls.Send();
        Response.Flush();
        Response.End();
    }

posted on 2009-05-26 17:29  大李  阅读(496)  评论(0)    收藏  举报

导航

立博名家