none
列印資料問題 RRS feed

  • 問題

  • 以下這段code是列印label
    我link了database後,把資料傳去excel列印

    如果資料多於14項,便需要在新一頁列印

    我想問為何第一頁的資料是可以順序的爪database的資料列印出來

    ,但為何第二頁開始資料會混亂?

     

     

    //  已成功connect database並成功得到資料

    for (int l=1; l<=t_page; l++)
                        {
                            //==========================================
                            // EXCEL
                      
                            Excel.Application excel = null;
                            Excel.Workbook wb = null;

                            object missing = System.Reflection.Missing.Value;

                            try
                            {
                                excel = new Excel.Application();
                                wb = excel.Workbooks.Open("D:\\excel_doc\\Statement_label.xls", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                                excel.Visible = true;
                                wb.Activate();
                            }
                            catch (COMException ex)
                            {
                                MessageBox.Show("Error accessing Excel: " + ex.ToString());

                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Error: " + ex.ToString());
                            }

                            Excel.Worksheet ws = null;
                            Excel.Range rng = null;

                            ws = (Excel.Worksheet)wb.ActiveSheet;

                           
                            for (int g = 0; g < totalRecg; g++)
                            {
                                string cust_label = dataTableg.RowsGift["no"].ToString();
                                                          

                                try
                                {

                                    SqlConnection conn1k = new SqlConnection(con_db.connStr_1);

                                    conn1k.Open();

                                    SqlParameter param1k = new SqlParameter("@knum", SqlDbType.VarChar, 15);
                                    param1k.Value = cust_label;

                                    SqlCommand myCommandk = new SqlCommand("select * from dbo.Customer where acNo = @knum"
                                            , conn1k);
                                    myCommandk.Parameters.Add(param1k);

                                    SqlDataAdapter dataAdapterk = new SqlDataAdapter(myCommandk);

                                    DataSet dsk = new DataSet();

                                    dataAdapterk.Fill(dsk, "progk");

                                    DataTable dataTablek = dsk.Tables["progk"];

                                    int totalReck = dataTablek.Rows.Count;

                                    string label_cname = dataTablek.Rows[0]["chiName"].ToString();
                                    string label_add1 = dataTablek.Rows[0]["add1"].ToString();
                                    string label_add2 = dataTablek.Rows[0]["add2"].ToString();

                                    label_check1 = g + 1;

                                   

                                    if (label_check1 == 1)
                                    {
                                       

                                        try
                                        {
                                            rng = ws.get_Range("B3", missing);
                                            rng.Value2 = label_cname;

                                            rng = ws.get_Range("B4", missing);
                                            rng.Value2 = label_add1;

                                            rng = ws.get_Range("B5", missing);
                                            rng.Value2 = label_add2;

                                            rng = ws.get_Range("B7", missing);
                                            rng.Value2 = "A/C NO:" + cust_label;

                                            excel.Visible = true;
                                            wb.Activate();

                                        }
                                        catch (COMException ex)
                                        {
                                            MessageBox.Show("Error accessing Excel: " + ex.ToString());
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show("Error: " + ex.ToString());
                                        }
                                    }

                                    string eng;

                                    double label_check2 = label_check1 % 2;

                                    if (label_check2 == 0)
                                    {
                                        try
                                        {
                                            rng = ws.get_Range("F" + label_check3, missing);
                                            rng.Value2 = label_cname;

                                            rng = ws.get_Range("F" + label_check4, missing);
                                            rng.Value2 = label_add1;

                                            rng = ws.get_Range("F" + label_check5, missing);
                                            rng.Value2 = label_add2;

                                            rng = ws.get_Range("F" + label_check6, missing);
                                            rng.Value2 = "A/C NO:" + cust_label;

                                            excel.Visible = true;
                                            wb.Activate();

                                        }
                                        catch (COMException ex)
                                        {
                                            MessageBox.Show("Error accessing Excel: " + ex.ToString());
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show("Error: " + ex.ToString());
                                        }

                                        label_check3 = label_check3 + 7;
                                        label_check4 = label_check4 + 7;
                                        label_check5 = label_check5 + 7;
                                        label_check6 = label_check6 + 7;
                                    }


                                    if (label_check2 != 0)
                                    {
                                        try
                                        {
                                            rng = ws.get_Range("B" + label_check3, missing);
                                            rng.Value2 = label_cname;

                                            rng = ws.get_Range("B" + label_check4, missing);
                                            rng.Value2 = label_add1;

                                            rng = ws.get_Range("B" + label_check5, missing);
                                            rng.Value2 = label_add2;

                                            rng = ws.get_Range("B" + label_check6, missing);
                                            rng.Value2 = "A/C NO:" + cust_label;

                                            excel.Visible = true;
                                            wb.Activate();

                                        }
                                        catch (COMException ex)
                                        {
                                            MessageBox.Show("Error accessing Excel: " + ex.ToString());
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show("Error: " + ex.ToString());
                                        }

                                    }

     

                                   

                                   

                                    //======================================

                                    //print out
                                    // Printout:

                                    if (label_check3 > 45)
                                    {

                                       
                                        object vk_missing1 = System.Reflection.Missing.Value;
                                        try
                                        {
                                            ws.PrintOut(vk_missing1, vk_missing1, vk_missing1, vk_missing1, vk_missing1, vk_missing1, vk_missing1, vk_missing1);
                                            //ws1.PrintPreview();                 
                                        }

                                        catch
                                        {
                                            //MessageBox.Show("未能成功列印");
                                        }
                                       


                                    
                                     label_check3 = 3;
                                     label_check4 = 4;
                                     label_check5 = 5;
                                     label_check6 = 7;


                                    
                                       
                                    }
                                }
                                catch (Exception m)
                                {
                                    MessageBox.Show(m.ToString(), "Error occurred while inserting data into database");
                                }
                            }

     

    2007年12月9日 下午 04:08