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;
}
}
}