Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
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);

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
Posted
Updated 1-Oct-12 9:38am
v2
Comments
skydger 1-Oct-12 16:04pm    
How does database data load to Excel?
Is it possible to optimize SQL query to exclude those empty spaces?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900