Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel

Status
Not open for further replies.

JamesHanley

Programmer
Jun 16, 2006
57
0
0
US
Ok, Next Move. My Data Grid Is Working Perfectly Now! Thanks To The Help Of You Wonderful People.

My Next Big Move Is To Export Information From Data Grid To Microsoft Excel. Is There An Easy Way To Do This? I Found Some Links On The Web, But They Were No Help. Also, I Need To Know Which "using" Feature To Use. Thank You.
 
Hi,

I do it cell by cell using my library:
myExcel.MyExcelSheet excel=new myExcel.MyExcelSheet();
excel.Show();
if(excel.Open("c:\\example.xls",1))
{
excel.ChooseSheet(1);
excel.WriteCell("B10","Hello");
excel.WriteCell("B11","World!");
excel.SaveAs("c:\\test.xls");
}
excel.CloseWorkbook();
excel.Close();

If You are intrested here is this library. You need to add two references: "Excel" (interop.Excel - Microsoft Excel 10.0 Object library)and "Microsoft.Office.Core" (interop.Microsoft.Office.Core - Microsoft Office 10.0 Object Library)

using System;
using Microsoft.Office.Core;
using System.Reflection;
using System.Collections;

namespace myExcel
{
/// <summary>
/// Pozwala grzebac w arkuszu Excel
/// </summary>
public class MyExcelSheet
{
private Excel.ApplicationClass app = null; // the Excel application.
private Excel.Workbook book = null;
private Excel.Worksheet sheet = null;
private Excel.Range range = null;

public MyExcelSheet()
{
try
{
app=new Excel.ApplicationClass();
}
catch(Exception ex)
{
MessageBox.Show("Error opening Excel "+Environment.NewLine+ex.Message);
}
}

public bool Show()
{
try
{
app.Visible = true;
app.ScreenUpdating = true;
app.DisplayAlerts = true;
}
catch(Exception ex)
{
MessageBox.Show("Error showing Excela"+Environment.NewLine+ex.Message);
return false;
}
return true;

}


public bool Open(string path,int sheetNo)
{
try
{
book = app.Workbooks.Open(path,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
sheet = (Excel.Worksheet)book.Worksheets[sheetNo];
}
catch(Exception ex)
{
MessageBox.Show("Error opening."+Environment.NewLine+ex.Message);
return false;
}
return true;
}

public bool ChooseSheet(int sheetNo)
{
try
{
sheet = (Excel.Worksheet)book.Worksheets[sheetNo];
}
catch(Exception ex)
{
MessageBox.Show("Error choosing "+sheetNo.ToString()+"."+Environment.NewLine+ex.Message);
return false;
}
return true;
}

public bool WriteCell(string adres,string myValue)
{
try
{
Excel.Range range1=sheet.get_Range(adres,adres);
range1.set_Value(Missing.Value,myValue);
}
catch
{
return false;
}
return true;
}

public string ReadCell(string adres)
{
try
{
Excel.Range range1=sheet.get_Range(adres,adres);
return range1.get_Value(Missing.Value).ToString();
}
catch
{
return "";
}
}

public bool SaveAs(string path)
{
try
{
book.SaveAs(path,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlShared,Missing.Value,
Missing.Value,Missing.Value,Missing.Value,Missing.Value);
}
catch(Exception ex)
{
MessageBox.Show("Error saving "+Environment.NewLine+ex.Message);
return false;
}
return true;
}

public bool CloseWorkbook()
{
try
{
book.Close(Missing.Value,Missing.Value,Missing.Value);
}
catch(Exception ex)
{
MessageBox.Show("Error closing workbook"+Environment.NewLine+ex.Message);
return false;
}
return true;
}


public bool Close()
{
try
{
app.Quit();
}
catch(Exception ex)
{
MessageBox.Show("Error closing Excela"+Environment.NewLine+ex.Message);
return false;
}
return true;
}

}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top