Excel Programming With C# [Resolved]

Posted by Sharpcnet under C# on 11/21/2013 | Points: 10 | Views : 1236 | Status : [Member] | Replies : 4
Is it possible to create an excel file and open it, without saving it first to server or local location. I'm trying to acheive this.
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;

protected void btnOpen_Click(object sender, EventArgs e)
{
OpenExcel();
}

private void OpenExcel()
{
Excel.Application app = new Excel.Application();
Excel.Workbook wb = new Excel.Workbook();
Excel.Worksheet ws = new Excel.Worksheet();
Excel.Range range = null;

app.visible = true;
wb = app.Workbooks.Add(1);
ws = wb.WorkSheets[1];
range = ws.get_Range("A1","A3");

ws.Cells[0,0]="Date";
ws.Cells[0,1]="Code";
ws.Cells[0,2]="Name";

}
Yes, the code is very much incomplete. The idea is to pop up an excel sheet (of certain template), to the user on button click. Could someone guide me, how I should proceed from here.




Responses

Posted by: Sharpcnet on: 11/21/2013 [Member] Starter | Points: 25

Up
0
Down

Resolved
It was all there and I didn't know. For beginers like me, hope this helps.
This Code will create a template in an excel sheet and open on a button click. The template chosen here is just a header with col. names.

Another important thing to note is, Office.Interop indexing starts from 1 instead of 0.

using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;

protected void btnOpen_Click(object sender, EventArgs e)
{
OpenExcel();
}

private void OpenExcel()
{
Excel.Application app = new Excel.Application();
Excel.Workbook wb = null;
Excel.Worksheet ws = null;
Excel.Range range = null;

app.visible = true;
wb = app.Workbooks.Add(1);
ws = (Excel.Worksheet)wb.WorkSheets[1];
range = ws.get_Range("A1","D1");

ws.Cells[1,1]="Date";
ws.Cells[1,2]="Code";
ws.Cells[1,3]="Name";

app.Rows.AutoFit();
app.Columns.AutoFit();
range.Borders.Color = System.Drawing.Color.Black.ToArgb();
range.Interior.Color = System.Drawing.Color.PeachPuff.ToArgb();
range.Font.Bold = true;
}


Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this once..
http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://csharp.net-informations.com/excel/csharp-open-excel.htm
http://www.codeproject.com/Articles/680421/Create-Read-Edit-Advance-Excel-2007-2010-Report-in#10

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 11/21/2013 [Member] Starter | Points: 25

Up
0
Down
Thank you but not helpful. Been there already. cant use a third party library here. And the rest..too high end for a novice.

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response