简体   繁体   English

将数据插入Excel电子表格并打印

[英]Insert data into excel spreadsheet and print

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. Excel工作表单元格映射到数据网格视图单元格。 User will click on a cell in data grid , then program will insert the new data into the excel sheet cell. 用户将单击数据网格中的一个单元格,然后程序会将新数据插入excel表格单元格中。

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? 1-是否可以将DB / datagridview数据写入电子表格字段,然后通过代码打印电子表格? 2- Or would I be better off creating a report in C# from scratch? 2-还是最好从头开始用C#创建报告?

You can use excel interop. 您可以使用excel interop。 Just add the reference to your project (in the com section). 只需将引用添加到您的项目(在com部分中)即可。 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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM