影子博客

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

//如果汇总的话直接可在模板里面填写公式,不过要有三行空行才行 比如SUM(A1,A2,A3)

fpSpread1.Sheets[0].RowCount = 30;
fpSpread1.Sheets[0].ColumnCount = 15;

        yclCheckManager cm = new yclCheckManager();
        DataSet ds= cm.getMaterialCheckProject(deliverNum);
        string pro_code = "";
        Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
        workbook.Open(Application.StartupPath + @"\Templates\原物料检验报告.xlt");
        sheet = workbook.Worksheets[0];


        if (ds.Tables[0].Rows.Count>0)
        {
            sheet.Cells["C5"].PutValue(ds.Tables[0].Rows[0]["p_name"].ToString());//赋值
            sheet.Cells["C6"].PutValue(ds.Tables[0].Rows[0]["shouHuoCode"].ToString());
            pro_code = ds.Tables[0].Rows[0]["pro_code2"].ToString();
            sheet.Cells["C4"].PutValue(ds.Tables[0].Rows[0]["pro_code"].ToString());
            sheet.Cells["O4"].PutValue(ds.Tables[0].Rows[0]["shouNum"].ToString());
            sheet.Cells["O5"].PutValue(ds.Tables[0].Rows[0]["goodNum"].ToString());
            sheet.Cells["O6"].PutValue(ds.Tables[0].Rows[0]["inDate"].ToString());
        }
        DataTable dt2=cm.get_Material(pro_code);
        if (dt2.Rows.Count>0)
        {
            sheet.Cells["H4"].PutValue(dt2.Rows[0]["material"].ToString());
        }
        if (ds.Tables[1].Rows.Count>0)
        {
            sheet.Cells["H5"].PutValue(ds.Tables[1].Rows[0]["product"].ToString());
        }
         sheet.Cells["L13"].PutValue(DateBasicInfo.userMessage.userName + " / " + DateTime.Now);
         sheet.Cells.ImportDataTable(ds.Tables[2], false, "A10");
           for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
           {
               sheet.Cells.Merge(9 + i,1,1,2);//合并单元格
               sheet.Cells.Merge(9 + i, 4,1,4);
               sheet.Cells.Merge(9 + i, 8,1, 4);
           }

        MemoryStream ms = workbook.SaveToStream();
        ms.Seek(0, SeekOrigin.Begin);
        fpSpread1.OpenExcel(ms);

        ms.Close();

fpSpread1.Sheets[0].RowCount = lastindex;
fpSpread1.Sheets[0].ColumnCount = 14;
//工段小计列合并
int sameNum = 0;
int gongduanNum = 0;
float sumnum7 = 0;
float sumnum8 = 0;
float sumnum9 = 0;
float sumnum10 = 0;
try
{
for (int i = 0; i < lastindex; i++)
{
if (fpSpread1.ActiveSheet.Cells[i, 0].Text.Contains("小计"))
fpSpread1.ActiveSheet.AddSpanCell(i, 0, 1, 3);
if (i > 3)
{

                    if (fpSpread1.ActiveSheet.Cells[i - 1, 0].Text == fpSpread1.ActiveSheet.Cells[i, 0].Text)
                    {
                        gongduanNum++;

                    }
                    else
                    {
                        if (gongduanNum > 0)
                        {
                            fpSpread1.ActiveSheet.AddSpanCell(i - gongduanNum - 1, 0, gongduanNum + 1, 1);
                            gongduanNum = 0;
                        }
                    }

                    if (fpSpread1.ActiveSheet.Cells[i - 1, 1].Text == fpSpread1.ActiveSheet.Cells[i, 1].Text)
                    {
                        sameNum++;
                        sumnum7 += float.Parse(fpSpread1.ActiveSheet.Cells[i - 1, 7].Text == "" ? "0" : fpSpread1.ActiveSheet.Cells[i - 1, 7].Text);//数据汇总
                        sumnum8 += float.Parse(fpSpread1.ActiveSheet.Cells[i - 1, 8].Text == "" ? "0" : fpSpread1.ActiveSheet.Cells[i - 1, 8].Text);
                        sumnum9 += float.Parse(fpSpread1.ActiveSheet.Cells[i - 1, 9].Text == "" ? "0" : fpSpread1.ActiveSheet.Cells[i - 1, 9].Text);
                        sumnum10 += float.Parse(fpSpread1.ActiveSheet.Cells[i - 1, 10].Text == "" ? "0" : fpSpread1.ActiveSheet.Cells[i - 1, 10].Text);
                    }
                    else
                    {
                        if (sameNum > 0)
                        {
                            sumnum7 += float.Parse(fpSpread1.ActiveSheet.Cells[i - 1, 7].Text == "" ? "0" : fpSpread1.ActiveSheet.Cells[i - 1, 7].Text);
                            fpSpread1.ActiveSheet.Cells[i - sameNum - 1, 7].Value = sumnum7;
                            fpSpread1.ActiveSheet.AddSpanCell(i - sameNum - 1, 7, sameNum + 1, 1);

                            sumnum8 += float.Parse(fpSpread1.ActiveSheet.Cells[i - 1, 8].Text == "" ? "0" : fpSpread1.ActiveSheet.Cells[i - 1, 8].Text);
                            fpSpread1.ActiveSheet.Cells[i - sameNum - 1, 8].Value = sumnum8;
                            fpSpread1.ActiveSheet.AddSpanCell(i - sameNum - 1, 8, sameNum + 1, 1);

                            sumnum9 += float.Parse(fpSpread1.ActiveSheet.Cells[i - 1, 9].Text == "" ? "0" : fpSpread1.ActiveSheet.Cells[i - 1, 9].Text);
                            fpSpread1.ActiveSheet.Cells[i - sameNum - 1, 9].Value = sumnum9;
                            fpSpread1.ActiveSheet.AddSpanCell(i - sameNum - 1, 9, sameNum + 1, 1);


                            sumnum10 += float.Parse(fpSpread1.ActiveSheet.Cells[i - 1, 10].Text == "" ? "0" : fpSpread1.ActiveSheet.Cells[i - 1, 10].Text);
                            fpSpread1.ActiveSheet.Cells[i - sameNum - 1, 10].Value = sumnum10;
                            fpSpread1.ActiveSheet.AddSpanCell(i - sameNum - 1, 10, sameNum + 1, 1);

                            fpSpread1.ActiveSheet.AddSpanCell(i - sameNum - 1, 1, sameNum + 1, 1);


                            sameNum = 0;
                            sumnum7 = 0;
                            sumnum8 = 0;
                            sumnum9 = 0;
                            sumnum10 = 0;
                        }
                    }
                }


            }
        }
        catch (Exception ex)
        {
           
        }
posted on 2016-05-10 15:41  影子博客  阅读(860)  评论(0编辑  收藏  举报