Jump to content
Sign in to follow this  
rshin2020

C#.Net Programme convert to Excel 2007

Recommended Posts

Hi. I have to write a simple program to display a console application for:
String - Voltage, EmployeeID
Number - 1.016, 00180000
Here's what ive done:


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
double p = 1.016;
int q = 00180000;

Console.WriteLine("Voltage: {0}", p);
Console.WriteLine("EmployeeID: {1}", q);
}


}
}


I seem to get an error but don't know where the error is. Could someone please help me to correct the code. I also need to convert the data to be displayed in Excel 2007. How do I do that. I'm really new to C#.net. Please help, thanks.

Share this post


Link to post
Share on other sites
Based on the code below, which part do I change? And from what I have read, it said that: 'Start by adding a new worksheet called "Tinned Goods" and adding some basic data and a simple calculation'. Meaning, do i have to type an example in a spreadsheet in excel? Or how?


using OfficeOpenXml; // namespace for the ExcelPackage assembly
b&
FileInfo newFile = new FileInfo(@"C:\mynewfile.xlsx");
using (ExcelPackage xlPackage = new ExcelPackage(newFile)) { b& }


ExcelWorksheet worksheet =
xlPackage.Workbook.Worksheets.Add("Tinned Goods");
// write some titles into column 1
worksheet.Cell(1, 1).Value = "Product";
b&
worksheet.Cell(4, 1).Value = "Peas";
worksheet.Cell(5, 1).Value = "Total";

// write some values into column 2
worksheet.Cell(1, 2).Value = "Tins Sold";

ExcelCell cell = worksheet.Cell(2, 2);
cell.Value = "15"; // tins of Beans sold
string calcStartAddress = // we want this for the formula
cell.CellAddress;
worksheet.Cell(3, 2).Value = "32"; // tins of Carrots sold
b&
worksheet.Cell(5, 2).Formula = string.Format(SUM({0}:{1})",
calcStartAddress, calcEndAddress);

Hope can help. Thanks.

Share this post


Link to post
Share on other sites
Still no reply. Anyway, here's what i have done. I need it in a console format. How do I do it using this code?

using System;
using System.Collections;
using System.Reflection; // For Missing.Value and BindingFlags
using System.Runtime.InteropServices; // For COMException
using Excel = Microsoft.Office.Interop.Excel;
using System.ComponentModel;
using System.Data;




namespace MSExcelApp
{
/// <summary>
/// Summary description for ExcelAuto.
/// </summary>
public class ExcelAuto
{
private Excel.Application ExcelApp;
private Excel.Workbook objBook;
private Excel.Worksheet objSheet;
private Excel.Range range;
string strAttendeeList, strAbsenteeList, strCopiesToList;
int totActionItems;
object oMissing, oTemplate;
private string strTitle;

public ExcelAuto()
{
//
// TODO: Add constructor logic here
//
}
public void CreateFile(ArrayList array)
{
object missing = System.Reflection.Missing.Value;
object fileName = "normal.dot";
object newTemplate = false;
object docType = 0;
object isVisible = true;

ExcelApp = new Excel.ApplicationClass();
ExcelApp.Visible = true;
objBook = ExcelApp.Workbooks.Add(missing);
objSheet = (Excel.Worksheet)objBook.Sheets["Sheet1"];
objSheet.Name = "It's Me";

objSheet.Cells[1, 1] = "Details";
objSheet.Cells[2, 1] = "Voltage : "+ array[0].ToString();
objSheet.Cells[3, 1] = "EmployeeID : "+ array[1].ToString();

objSheet.get_Range("A1", "A1").Font.Bold = true;
objSheet.get_Range("A1", "A6").EntireColumn.AutoFit();
objSheet.get_Range("A1","A7").BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlMedium,
Excel.XlColorIndex.xlColorIndexAutomatic,Excel.XlColorIndex.xlColorIndexAutomatic);
}

static void Main()
{
Application.Run(new Form1());
}

private void btnCreate_Click(object sender, System.EventArgs e)
{
ExcelAuto excel = new ExcelAuto();
ArrayList array = new ArrayList();
bool filled = true;

if ( ( 1.016).ToString().Length.Equals(0) )
{
errorProvider.SetError(1.016,"cannot be empty" );
filled = false;
}
if( ( 00180000 ).ToString().Length.Equals(0) )
{
errorProvider.SetError( 00180000,"cannot be empty" );
filled = false;
}


if ( filled == true )
{
array.Insert(0,1.016);
array.Insert(1,00180000);


excel.CreateFile(array);
}
}

private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...