.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Interview Question >> .NET Framework >> Post New Question Subscribe to Interview Questions

how to add background to excel using C#

Posted By :Sreeraj Nair Mungath     Posted Date :July 16, 2012    Points :40   Category :.NET Framework 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace excel2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlWorkSheet.SetBackgroundPicture(@"C:\Users\snair02\Desktop\SRJ DOCS\a.jpg");

//add some text
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkSheet.Cells[2, 1] = "Adding background in Excel File";


xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);

MessageBox.Show("File created !");
}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}

}



}




}



You can also find related Interview Question to how to add background to excel using C#  below: 

What does AddressOf operator do in background ?

  
The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the execution of the code by calling the Start() method of the thread. (More...)

How to set a HTML document's background color in JavaScript?

  
Using document object the back ground color can be changed by JavaScript. The following example illustrates it.





. (More...)

In How many ways Data can be Export from Sql Table to Excel Sheet?

  
Some of these options include
1) Data Transformation Services (DTS),
2) SQL Server Integration Services (SSIS)
3) Bulk Copy (BCP)
4)OPENROWSET() (More...)

How to Change the BackGround Color of a Button dynamically in WPF?

  
System.Windows.Controls.Button b = new System.Windows.Controls.Button();

b.Content = "OK";

b.Background = System.Windows.Media.Brushes.Red;
(More...)

What is the difference between Value and Value2 when refering to to a range or cell. ( EXCEL )

  
The difference between Value and Value2 is that....

1) Value2 always uses the same value that Excel uses. It
does not truncate decimal places like when you divide 22 by 7 and format the cell for 2 decimal places, value 2 will give you the maximum number of decimal places in doing the calculation.

2) Value2 does not use the date or currency data types. If you enter 01/01/2006 in cell and then retrieve Selection.Value and Selection.Value2 you will get 01/01/2006 and 38718 respectively.

(More...)

What does AddressOf operator do in background ?

  
The AddressOf operator creates a delegate object to the BackgroundProcess method. A
delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread
has been instantiated, you begin the execution of the code by calling the Start() method
of the thread (More...)

How to insert single quote at the start and end of every column in excel sheet.

  
use the below formula in the result column like belwo-

Suppose in A2 you have 500 and in B2 you want '500' then do the following-
B2= "'"&A2&"'" (More...)

What does the Power View Add-in for Excel do?

  
Power View ("Crescent") enables users to visualize and interact with modeled data by using highly interactive visualizations, animations and smart querying. (More...)

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

 
Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.


Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend