0
votes

My problem is:

I have been provided a spreadsheet that simulates what the person wants to be printed from the application. Excel sheet cells are mapped into data grid view cells. User will click on a cell in data grid , then program will insert the new data into the excel sheet cell.

Then the sheet should be printed .

My question is:

1- Is it possible to write DB/datagridview data to the spreadsheets fields then print the spreadsheet via code? 2- Or would I be better off creating a report in C# from scratch?

1
Can you post some of your code please? - sarwar026

1 Answers

1
votes

You can use excel interop. Just add the reference to your project (in the com section). You can then write in a file.

using Excel = Microsoft.Office.Interop.Excel;

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

xlApp = new Excel.ApplicationClass(); 
xlWorkBook = xlApp.Workbooks.Open(_filename, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

//Attribute a value to a cell
var cell = (Range)xlWorkSheet .Cells[row, column];
cell.Value2 = "Test";

//This should print
xlWorkBook.PrintOut (Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

xlWorkBook.Close(false, misValue, misValue); 
xlApp.Quit();