Write data from textbox to existing excel file and print it out using c#
-
Sunday, May 13, 2012 4:47 PM
Hi to everyone!I have i form and a couple of textboxes on it and a button. I want to write the text from textboxes to existing excel file and print it out. Can you help me with any code samples? I want to put the code in button click event handler.
Please help!!!
qqq
All Replies
-
Monday, May 14, 2012 2:01 AM
Hi AlexAnB,
You need clarify where your textboxes are and clarify your Excel version, VSTO run-timer version for further research.
Best Regards,
T.X.
-
Monday, May 14, 2012 6:19 AMModerator
Hi qqq
Are you using VSTO? If not, this question should be in the Excel for Developers forum?
Please also specify the programming language and version of Excel involved as, no matter where the question is placed, this information is necessary in order to assist you.
Cindy Meister, VSTO/Word MVP
-
Monday, May 14, 2012 2:01 PM
I am sorry if i posted my question in wrong thread..
My project is on the Visual C# .Net Framework 4. (Visual Studio 2010). The version of excel it doesnt't matter to me. I can convert my excel template to 2010 version if it will be nessesary. I have a form with 5 comboboxes binded to sql database and 4 textboxes where the user have to type text by himself. On the button click event i need to write all text values from comboboxes and textboxes and print out this template. Can you help me with code please and wich version of interop and what kind of namespases i will have to connect to my project. I tried to find out how to do it in VSTO, but have no idea. Because all code samples i found works only inside VSTO project and i cannot find the way how to write data to existing excel file directly from C# code. If you need more information about my project.. just let me know..
Any help will be appreciated.. thanks
qqq
-
Tuesday, May 15, 2012 1:57 AM
Import/export Excel worksheet in ASP.NET Sample
http://code.msdn.microsoft.com/CSASPNETExcelImportExport-71cf1101#content
http://vsto.tistory.com
-
Tuesday, May 15, 2012 5:48 AMModerator
Hi qqq
VSTO is designed to run in-process with the Office application and to extend the possibilities of the UI. So it's not the best technology for what you want to do.
If I understand you correctly, what you basically need to do is generate a new Excel workbook from data the user chooses?
There are a number of possible approaches. Which one to choose is a question of how the worksheet needs to be populated.
1. Working with Excel like a database: VSTO Beginner gives you a link to a sample that uses this approach. Basically, it means creating an OLE DB connection to the workbook and writing data into it. This is useful if your end result should be a table of data (rows and columns).
2. Working directly with the Open XML file: The Open XML file format introduced in Office 2007 allows you to create new Excel files from scratch, or manipulate existing files (a template, for example). This gives you a much finer control over where data is placed. It also allows you to work with objects such as diagrams and graphics.
3. "Interop": Your code works with the Office APIs. This will be slower than the other two approaches and has other drawbacks - you should only go this route if neither of the others will work for you. The sample code you've found for VSTO uses these APIs, which is correct, because VSTO runs in-process.
Does this information help with your decision?
Cindy Meister, VSTO/Word MVP
- Marked As Answer by Tom_Xu_WXModerator Tuesday, June 05, 2012 6:59 AM
-
Tuesday, May 15, 2012 7:13 AM
Thank you very much! Your answer was really informative. I found a solution. Here i can load existing excel file from code and write data to it cells, that what i really needed:
Excel.Application myExcelApp; Excel.Workbooks myExcelWorkbooks; Excel.Workbook myExcelWorkbook; object misValue = System.Reflection.Missing.Value; myExcelApp = new Excel.ApplicationClass(); myExcelApp.Visible = true; myExcelWorkbooks = myExcelApp.Workbooks; String fileName = "C:\\book1.xls"; \\ set this to your file you want myExcelWorkbook = myExcelWorkbooks.Open(fileName, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue); Excel.Worksheet myExcelWorksheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet; String cellFormulaAsString = myExcelWorksheet.get_Range("A2", misValue).Formula.ToString(); \\ this puts the formulaNow i have another problem:
Can you help me to print out this document from code (I need 4 copies, and duplex printer mood) and close it without saving any changes after printing..qqq
-
Tuesday, May 15, 2012 12:08 PMModerator
Hi qqq
As this is a new question, I recommend you start a new message thread. What's more, as you're not using the VSTO technology, you should ask it in the Excel for Developers forum. You'll find many more people there who specialize in Excel :-)
http://social.msdn.microsoft.com/Forums/en-US/exceldev/threads
If you have a moment, please mark a message in this discussion as "the answer".
Cindy Meister, VSTO/Word MVP
-
Wednesday, May 16, 2012 7:03 AMLook at the code please. This is Interop. methods. Why i should repost my question in another thread?
qqq
-
Wednesday, May 16, 2012 8:26 AMbecause this forum is about VSTO - specific component used to extend office apps to run your code. Interop is orthogonal to this as you can also use to for stright automation, without VSTO. Cindy merely suggests that your question will be faster answered on forum where people with specific excel knowledge roam.

