Example - Working with MS Excel:

using MSExcel = TcKs.MSOffice.Excel;
...
...
...
// The thread which communicate with MS Office must have a same culture as the MS Office.
// It's a well-know bug for several versions of MS Office.
// This bug is not in MS Office MUI.
// My language mutation of MS Office is "en-US".
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo( "en-US" );

using ( MSExcel.Application app = MSExcel.Application.CreateApplication() ) {
	MSExcel.Workbook book1 = app.Workbooks.Open( this.txtOpen_FilePath.Text );
	MSExcel.Worksheet sheet1 = (MSExcel.Worksheet)book1.Worksheets[1];
	MSExcel.Range range_A1 = sheet1.GetRange( "A1" );
	MSExcel.Range range_B2 = sheet1.GetRange( "B2" );

	string message = "In cell 'A1' is '{0}' and in cell 'B2' is '{1}'.";
	message = string.Format( message, range_A1.Value, range_B2.Value );

	MessageBox.Show( message );
}

// Forced garbage collection. This is not necessary, but usefull.
TcKs.MSOffice.Common.WrapperHelper.GCCollect();

Last edited Oct 8, 2008 at 5:03 PM by tencokacistromy, version 1

Comments

satyajitn Apr 13, 2010 at 4:28 AM 
Hi

Can you please give example of sheet.delete or row delete fuctionality. I am using this libary for excle reporting however I am not able to delete unwanted sheet or rows

internal void writeStateCummulativeReportbysites(DataSet dsReport, string reportpath)
{
int templatesheet;
string sheet_new_name;

for (int i = 0; i < dsReport.Tables.Count; i = i + 2)
{
templatesheet = int.Parse(dsReport.Tables[i].Rows[0].ItemArray[16].ToString());
sheet_new_name = dsReport.Tables[i].Rows[0].ItemArray[15].ToString();


System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

using (MSExcel.Application app = MSExcel.Application.CreateApplication())
{
MSExcel.Workbook book1 = app.Workbooks.Open(reportpath);
MSExcel.Worksheet sheet1 = (MSExcel.Worksheet)book1.Worksheets[templatesheet];
sheet1.Name = sheet_new_name;
MSExcel.Range range1 = sheet1.GetRange("T9", "AB59");
//MSExcel.Range range2 = sheet1.GetRange("AF25", "AP37");


sheet1.GetRange("A4").Value2 = dsReport.Tables[i + 1].Rows[0].ItemArray[9].ToString();
sheet1.GetRange("C1").Value2 = dsReport.Tables[i].Rows[0].ItemArray[2].ToString();

object[,] ar = new object[51, 9];

ar = (object[,])range1.Value2;

for (int j = 0; j <= dsReport.Tables[i + 1].Rows.Count - 1; j++)
{

ar[j + 1, 1] = dsReport.Tables[i + 1].Rows[j].ItemArray[2].ToString(); //statename
ar[j + 1, 2] = int.Parse(dsReport.Tables[i + 1].Rows[j].ItemArray[3].ToString()); //startlead
ar[j + 1, 3] = int.Parse(dsReport.Tables[i + 1].Rows[j].ItemArray[4].ToString());//bad
ar[j + 1, 4] = int.Parse(dsReport.Tables[i + 1].Rows[j].ItemArray[5].ToString());//completes
ar[j + 1, 5] = int.Parse(dsReport.Tables[i + 1].Rows[j].ItemArray[6].ToString());//contacts
ar[j + 1, 6] = int.Parse(dsReport.Tables[i + 1].Rows[j].ItemArray[7].ToString());//sale
ar[j + 1, 7] = decimal.Parse(dsReport.Tables[i + 1].Rows[j].ItemArray[8].ToString()); // hrs
ar[j + 1, 9] = int.Parse(dsReport.Tables[i + 1].Rows[j].ItemArray[9].ToString()); // bounce


}

range1.Value2 = ar;
book1.Save();
TcKs.MSOffice.Common.WrapperHelper.GCCollect();
}
}

}

tencokacistromy Nov 10, 2009 at 2:11 PM 
Unfotunately, in current version no. But it is planned for future versions.

fantomaspce Sep 21, 2009 at 1:33 PM 
Good morning.
It is possible collect field as in Excelu through the medium numbers columns (for example Sheets("List1").Cells(1, 2).Value)?
Thank Thomas