Excel模板批量打印C#Windows应用程序 [英] Excel Template Bulk Printing C# Windows Application
问题描述
我一直在使用C#开发Windows应用程序.我的要求是打印带有给定订单号的数据库值的预定义Excel模板.意味着,我必须从数据库(SQL Server)中检索数据,并用数据库值替换预定义的单元格值(文本)(在Excel模板中).例如-
在Excel单元格中,值"SENDER_NAME"将替换为"FEDEX","OrderNo"将替换为"1212",依此类推.我正在使用以下代码来做到这一点.
I have been working on a Windows application in C#. My Requirement is to print a predefined Excel Template with database values for a given Order Number. Means, i have to retrieve data from database(SQL Server) and replacing predefined cell values(Text) (in excel template) with database value. For Example -
In Excel-cell value "SENDER_NAME" will be replaced by "FEDEX", "OrderNo" will be replaced by "1212" and so on. I am using following code to do that.
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);
xlWorkSheet.Cells.Replace("SENDER_NAME", "FEDEX", missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
我的问题是-当excel中给定单元格文本的数据库中不存在某些值时,其值替换为NULL(或空白值),因此在打印时留有空格.我必须减少那些空白?
有什么办法吗?谢谢
My PROBLEM is - When some values are not present in database for a given cell text in excel , its replaced by NULL(or blank value), so it lefts spaces while printing. I have to cut down those emptyspaces??
Any solution? Thanks
推荐答案
在循环中...
将数据库中的字段值逐行分配给变量.检查变量是否为null,如果为null,则相应地设置变量格式.然后将变量传递到电子表格中的一行单元格.
In a loop...
Assign the field values from the database, row by row, to variables. Check the variables for null, if null, format the variable accordingly. Then pass the variable to a row of cells in the spreadsheet.
这篇关于Excel模板批量打印C#Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!