0

我需要在运行时使用 c# 创建基于 excel 模板的运输标签。

现在我有工作系统,我正在用数据库值替换 excel 模板中的字段名称并将其发送以进行打印。

但是如果数据库中的值为空,它会创建空间。我不想要那个空间。在单个 excel 行中,需要替换 2 个以上的单元格,因此如果不存在值,那么我只需要删除单元格(因为我无法删除行)并将其正下方的单元格移动到该位置或合并它与下面的单元格。

如果您有更好的方法来生成基于 excel 模板的运输标签,请告诉我。谢谢

                Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.ApplicationClass();

            //string path = AppDomain.CurrentDomain.BaseDirectory + @printLabelName;
            string fileLocation = "";
            DataTable dtPath = clsData.getFileLocations();
            if (dtPath.Rows.Count != 0)
            {
                //ctbExport.Text = dt.Rows[0]["exportFileLocation"].ToString();
                fileLocation = dtPath.Rows[0]["printTemplateLocation"].ToString() + "\\";
            }

            String[] orderList = ctbOrderNo.Text.Split(' ');
            for (int i = 0; i < orderList.LongLength; i++)
            {
                string strOrderNo = orderList[i];

                int val = clsData.updOrderDataBulkPrint(Convert.ToDecimal(ctbCustomValue.Text), cmbCustomType.Text, strOrderNo);

                DataTable dt = clsData.getOrderDetailByOrderNo(strOrderNo);
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("Order No. " + strOrderNo + " doesn't exist");
                    return;
                }
                DataTable dtSender = clsData.getSenderDataById(Convert.ToInt32(cmbSender.SelectedValue));
                if (dtSender.Rows.Count > 0 && dt.Rows.Count > 0)
                {
                    //We will name shipping label excel template file names as number for e.g. 1 , 2 , 3 , 4 , 4 etc. 
                    //user will put of these values and then system will know which shipping label excel template to use for all these orders.
                    string printLabelName = numUpDownPrintLabelRype.Value.ToString() + ".xlsx";

                    //string path = AppDomain.CurrentDomain.BaseDirectory + @printLabelName;                       
                    string path = fileLocation + @printLabelName;

                    bool isPathCorrect = true;
                    try
                    {
                        object missingValue = Type.Missing;

                        xlWorkBook = xlApp.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                        //MessageBox.Show(xlWorkSheet.get_Range("A1", "A1").Value2.ToString());                                           
                        xlWorkSheet.Cells.Replace("SenderName", dtSender.Rows[0]["senderName"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("SenderCompanyName", dtSender.Rows[0]["senderCompanyName"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("SenderAdressLine1", dtSender.Rows[0]["senderAdressLine1"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("SenderAddressLine2", dtSender.Rows[0]["senderAddressLine2"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("SenderCity", dtSender.Rows[0]["senderCity"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("SenderState", dtSender.Rows[0]["senderState"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("SenderZIP", dtSender.Rows[0]["senderZIP"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("SenderCountry", dtSender.Rows[0]["senderCountry"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("OrderNumber", dt.Rows[0]["orderNo"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("Zone", dt.Rows[0]["zone"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);

                        xlWorkSheet.Cells.Replace("CustomerName", dt.Rows[0]["customerName"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);

                        xlWorkSheet.Cells.Replace("CustomerCompanyName", dt.Rows[0]["cCompanyName"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);

                        object misval = Type.Missing;



                      //Replace values by database values here


                        xlWorkSheet.Cells.Replace("CustomerAddressLine1", dt.Rows[0]["cAddressLine1"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("CustomerAddressLine2", dt.Rows[0]["cAddressLine2"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("CustomerAddressLine3", dt.Rows[0]["cAddressLine3"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("CustomerAddressCity", dt.Rows[0]["cAddressCity"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("CustomerAddressState", dt.Rows[0]["cAddressState"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("CustomerAddressZIP", dt.Rows[0]["cAddressZIP"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("CustomerAddressCountry", dt.Rows[0]["cAddressCountry"].ToString(), missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);

                        xlWorkSheet.Cells.Replace("ProductType", cmbProdType.Text, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("CustomsType", cmbCustomType.Text, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
                        xlWorkSheet.Cells.Replace("CustomsValue", ctbCustomValue.Text, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);

                        xlWorkBook.PrintOut(1, 1, 1, false, misValue, misValue, misValue, misValue);

                        xlWorkBook.Close(false, misValue, misValue);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Can't open Label Template, please check location.");
                        isPathCorrect = false;
                    }
                }
                else {
                    MessageBox.Show("This Sender doesn't exist.");
                }
            }
            xlApp.Quit();
4

1 回答 1

1

因此,如果值不存在,那么我只需要删除单元格(因为我无法删除行)并将其正下方的单元格移动到该位置或将其与下面的单元格合并

您想使用.Range.Delete(Shift)Excel PIA DLL(我确定您已经在使用的互操作 DLL)中给出的方法。

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.delete(v=office.11​​).aspx

xlDeleteShift方向

找到要删除的单元格后,请使用以下内容(从您的代码示例中提取的部分):

未经测试

xlWorkSheet.get_Range("A1", "A1").Delete(XlDeleteShiftDirection.xlShiftUp);
于 2012-10-03T06:58:11.100 回答