Excel Tabelle in VB einlesen

dan7

Grünschnabel
Hallo alle,
Ich möchte gerne excel tabelle mit einem c/c++ VB programm lesen oder schreiben. Ich habesowas noch nie programmiert. Können sie mir eine Lösung finden.
Danke im voraus
 
Hallo,

leider habe ich nicht viel Zeit, meinen Code zu dokumentieren(ist auch schon eine Ewigkeit her). Die Klasse "clsExcel" ist zwar in C# aber die Codierung in VB ist ähnlich. Die Funktionsnamen dürften selbstredend sein. Wichtig ist, dass du im Projekt den richtigen Verweis mit einbindest(weiß nicht genau was im Moment die aktuellste ist).

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace TxtToXls
{
class clsExcel
{
private string m_strExcelFile = String.Empty;

public bool CreateNewExcelFile()
{

string strCaption = this.GetType().ToString() + ".CreateNewExcelFile(...)";


try
{
Excel.Application xlsAppl = new Excel.Application();
//xlsAppl.Visible = true;
xlsAppl.Workbooks.Add(Type.Missing);
xlsAppl.DisplayAlerts = false;
for (int ii = 3; ii > 1; ii--)
{
((Excel.Worksheet)xlsAppl.ActiveWorkbook.Sheets[ii]).Delete();

}


xlsAppl.DisplayAlerts = false;
xlsAppl.ActiveWorkbook.SaveAs(m_strExcelFile, Type.Missing, Type.Missing, Type.Missing,
false, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);

xlsAppl.Workbooks[1].Close(false, m_strExcelFile, Type.Missing);
xlsAppl.Quit();

return true;
}

catch (Exception exp)
{
MessageBox.Show("Fehler: " + exp.Message, strCaption);
return false;
}

finally
{
// ...
}

}


public void TextAppendText(string strRead, string strWrite)
{

string strCaption = this.GetType().ToString() + ".TextAppendText(...)";
StreamReader srRead = null;
StreamWriter swWrite = null;
string strLine;
try
{
srRead = new StreamReader(strRead);
strLine = srRead.ReadLine();
strLine = srRead.ReadLine();

strLine = srRead.ReadToEnd();
swWrite = File.AppendText(strWrite);
swWrite.WriteLine(strLine);
swWrite.Flush();
swWrite.Close();


srRead.Close();
}
catch (Exception exp)
{
MessageBox.Show("Fehler: " + exp.Message, strCaption);
}
}


public bool CopyTextToExcelfile(string strTxtToCopy, string strExcelfile, int intSheet)
{

string strCaption = this.GetType().ToString() + ".CopyTextToExcelfile(...)";
Excel.Application xlsAppl = new Excel.Application();
//xlsAppl.Visible = true;

try
{
xlsAppl.Workbooks.Open(strExcelfile, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlsAppl.Workbooks.OpenText(strTxtToCopy, Type.Missing, 1, Excel.XlTextParsingType.xlDelimited,
Excel.XlTextQualifier.xlTextQualifierNone, Type.Missing, Type.Missing, true, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);


((Excel.Worksheet)(xlsAppl.Workbooks[2].Sheets[1])).Copy(Type.Missing,
xlsAppl.Workbooks[1].Sheets[intSheet]);
strTxtToCopy = strTxtToCopy.Replace("txt", "xls");
xlsAppl.Workbooks[2].Close(true, Type.Missing, Type.Missing);
xlsAppl.Workbooks[1].Close(true, Type.Missing, Type.Missing);
xlsAppl.Quit();

return true;
}

catch (Exception exp)
{
MessageBox.Show("Fehler: " + exp.Message, strCaption);
System.Diagnostics.Debug.Assert(false);
return false;
}
}


public void DeleteSheet(string strExcelfile, int intSheet)
{

string strCaption = this.GetType().ToString() + ".DeleteSheet(...)";
Excel.Application xlsAppl = new Excel.Application();

try
{
xlsAppl.Workbooks.Open(strExcelfile, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
xlsAppl.DisplayAlerts = false;
((Excel.Worksheet)xlsAppl.ActiveWorkbook.Sheets[intSheet]).Delete();
xlsAppl.Workbooks[1].Close(true, Type.Missing, Type.Missing);
xlsAppl.Quit();
}

catch (Exception exp)
{
MessageBox.Show("Fehler: " + exp.Message, strCaption);
System.Diagnostics.Debug.Assert(false);
}

}




}
}

Gruß manlin
 
Ich hatte schon den link vongoogle gefunden. Ich bearbeite es im mom. Aber finde nicht viele include dateien
 
Zurück