[英]excel integration with c# .net
I needs to create shipping lable based on the excel template at run time using the c#. 我需要在运行时使用C#基于excel模板创建运输标签。
Right now I have working system, where I am replacing the fields name in excel template with database value and sending it for printing. 现在,我有了一个工作系统,在这里我要用数据库值替换excel模板中的字段名称,并将其发送以进行打印。
But It is creating space if value is null in database. 但是,如果数据库中的值为null,则正在创建空间。 I don't want that space.
我不要那个空间。 In a single excel row, there are more than 2 cells needs to be replaced so if value is not present then I needs to delete the cell only(as I can't delete row) and shift its just below cell to that position or merge it with below cell.
在单个excel行中,需要替换2个以上的单元格,因此,如果不存在值,则仅需要删除该单元格(因为我无法删除行),并将其正好位于单元格下方移至该位置或合并它与下面的单元格。
If you have any better way to generate shipping lable based on excel template then let me know that.Thanks 如果您有更好的方法基于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();
so if value is not present then I needs to delete the cell only(as I can't delete row) and shift its just below cell to that position or merge it with below cell
所以如果值不存在,那么我只需要删除单元格(因为我不能删除行),然后将其正下方的单元格移动到该位置或与下方的单元格合并
You want to use the .Range.Delete(Shift)
method that is given in the Excel PIA DLLs (the interop DLLs which I'm sure you're already using). 您要使用Excel PIA DLL(我确定您已经在使用的互操作DLL
.Range.Delete(Shift)
的.Range.Delete(Shift)
方法。
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.delete(v=office.11).aspx http://msdn.microsoft.com/zh-CN/library/microsoft.office.interop.excel.range.delete(v=office.11).aspx
xlDeleteShiftDirection xlDeleteShiftDirection
Once you find the cell that you want to delete, use something like this (which part of was lifted from your code example): 找到要删除的单元格后,请使用以下代码(该部分的内容已从代码示例中删除):
xlWorkSheet.get_Range("A1", "A1").Delete(XlDeleteShiftDirection.xlShiftUp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.