Подскажите как работать с excel 2003. С 2002-м получается, а с 2003-м никак.
Здравствуйте, Ovsyannikov, Вы писали:
O>Подскажите как работать с excel 2003. С 2002-м получается, а с 2003-м никак.
Пример от AntiXtiSt
/// <summary>
/// Заполненя таблицы данными с excel-листа
/// </summary>
/// <param name="strSheetName">Имя листа [+ набор ячеек]</param>
/// <returns>Объект — таблица</returns>
public System.Data.DataTable getData(string strSheetName)
{
if (strSheetName == "")
strSheetName = combo.Items[combo.SelectedIndex].ToString();
// Избавляет нас от глюка Excel-я
System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
#region Work
Excel.ApplicationClass app = null;
Excel.Workbook book = null;
Excel.Worksheet sheet = null;
Excel.Range range = null;
try
{
app = new Excel.ApplicationClass();
}
catch(Exception ex)
{
app = (Excel.ApplicationClass)Marshal.GetActiveObject("Excel.Application");
string s = ex.Message;
}
try
{
app.Visible = false;
app.ScreenUpdating = false;
app.DisplayAlerts = false;
book = app.Workbooks.Open(this.Filename, 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);
//Отличие версий (новая — 15 параметров)
//, Missing.Value, Missing.Value);
//sheet = (Worksheet)book.Worksheets[(object)strSheetName];
sheet = (Excel.Worksheet) book.Worksheets.get_Item(1);
// get a range to work with
range = sheet.get_Range("A1", Missing.Value);
// get the end of values to the right (will stop at the first empty cell)
range = range.get_End(Excel.XlDirection.xlToRight);
// get the end of values toward the bottom, looking in the last column (will stop at first empty cell)
range = range.get_End(Excel.XlDirection.xlDown);
// get the address of the bottom, right cell
string downAddress = range.get_Address(
false, false, Excel.XlReferenceStyle.xlA1,
Type.Missing, Type.Missing);
// Get the range, then values from a1
range = sheet.get_Range("A1", downAddress);
System.Data.DataTable dt = new System.Data.DataTable(strSheetName);
object[,] values = (object[,])range.Value2;
for (int col = 1; col <= values.GetLength(1); col++)
dt.Columns.Add(new DataColumn(values[1, col].ToString()));
object[] oo = new object[values.GetLength(1)];
for (int row = 2; row <= values.GetLength(0) — 2; row++)
{
// 3 — колонка в которой пустая клетка означает конец всего считывания
if (values[row, 3] == null) break;
for (int col = 1; col <= values.GetLength(1); col++)
oo[col — 1] = values[row, col];
dt.Rows.Add(oo);
}
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
return dt;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
range = null;
sheet = null;
if (book != null)
book.Close(false, Missing.Value, Missing.Value);
book = null;
if (app != null)
app.Quit();
app = null;
}
#endregion
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
return null;
}