Wie Verwenden Von OLE DB Zu Erstellen, Die Excel-Datenbank

Meine Aufgabe ist es, nehmen Sie eine Access-Datenbank und erstellen Sie eine Excel-Datei, aber ich kann nicht scheinen, um ERSTELLEN die Excel-Datei, die die OLE DB verwenden.

Die Excel-Datei-name wird von der Ingenieur, der das tool ausführt. Jede Daten-Tabelle in die Access-Datenbank wird ein Arbeitsblatt in der Excel-Datei.

Jetzt habe ich ein sausen ich kann nicht mehr: Wenn die Excel-Datei nicht vorhanden ist, ich kann es schaffen!

internal const string XL_FMT = "Provider=Microsoft.{0}.OLEDB.{1};Data Source={2};Mode=ReadWrite;Extended Properties=\"Excel {1};HDR={3};IMEX=1;\"";
internal DataTable tableNames;
internal OleDbConnection oleCon;
private string conStr;

public OleBase(string connectionString) {
  conStr = connectionString;
  //Using the debugger, conStr is:
  //"Provider=Microsoft.ACE.OLEDB.12.0;" +
  //"Data Source=C:\\Users\\cp-jpool\\Documents\\Ecat5.xlsx;" +
  //"Mode=ReadWrite;Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1;\""
  object[] param = new object[] { null, null, null, "TABLE" };
  oleCon = new OleDbConnection(conStr);
  oleCon.Open();
  tableNames = oleCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, param);
}

Wenn die Excel-Datei NICHT vorhanden ist, immer wenn ich call Open() bekomme ich die folgende OleDbException:

"Das Microsoft Access-Datenbankmodul konnte das Objekt nicht finden "C:\Users\cp-jpool\Documents\Ecat5.xlsx'. Stellen Sie sicher, dass das Objekt existiert und dass Sie buchstabieren seinen Namen und den Pfadnamen richtig. Wenn 'C:\Users\cp-jpool\Documents\Ecat5.xlsx' kein lokales Objekt, prüfen Sie Ihre Netzwerkverbindung oder Kontaktieren Sie den server-administrator."

So, die Datei nicht vorhanden, huh? Gut, ich habe versucht, erstellen, ändern meine CTor() werden:

public OleBase(string connectionString) {
  conStr = connectionString;
  object[] param = new object[] { null, null, null, "TABLE" };
  oleCon = new OleDbConnection(conStr);
  if (-1 < conStr.IndexOf(";IMEX=1;")) {
    string dsString = "Data Source=";
    int dsIndex = conStr.IndexOf(dsString);
    string conSub = conStr.Substring(dsIndex + dsString.Length);
    int firstCol = conSub.IndexOf(';');
    string xlPath = conSub.Substring(0, firstCol);
    if (!File.Exists(xlPath)) {
      File.Create(xlPath);
    }
  }
  oleCon.Open();
  tableNames = oleCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, param);
}

Nun, wenn dieser code versucht, um die Open() - Methode des OleDbConnection, bekomme ich diese verschiedenen OleDbException:

"Das Microsoft Access-Datenbankmodul kann nicht öffnen oder schreiben der Datei ". Es ist bereits exklusiv geöffnet, die von einem anderen Benutzer, oder Sie benötigen die Berechtigung zum anzeigen und schreiben von Daten."

Ich sogar versucht, erstellen der Excel-Datei mit einem StreamWriter zu füllen Sie es mit basic Header:

public OleBase(string connectionString) {
  conStr = connectionString;
  object[] param = new object[] { null, null, null, "TABLE" };
  oleCon = new OleDbConnection(conStr);
  if (-1 < conStr.IndexOf(";IMEX=1;")) {
    string dsString = "Data Source=";
    int dsIndex = conStr.IndexOf(dsString);
    string conSub = conStr.Substring(dsIndex + dsString.Length);
    int firstCol = conSub.IndexOf(';');
    string xlPath = conSub.Substring(0, firstCol);
    using (StreamWriter xls = new StreamWriter(xlPath, false, Encoding.UTF8)) {
      xls.WriteLine("<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?>");
      xls.WriteLine("<ss:Workbook xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" ");
      xls.WriteLine("xmlns:o=\"urn:schemas-microsoft-com:office:office\" ");
      xls.WriteLine("xmlns:x=\"urn:schemas-microsoft-com:office:excel\" ");
      xls.WriteLine("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
      xls.WriteLine("<ss:Styles>");
      xls.WriteLine("<ss:Style ss:ID=\"Default\" ss:Name=\"Normal\"><ss:Alignment ss:Vertical=\"Bottom\"/><ss:Borders/><ss:Font/><ss:Interior/><ss:NumberFormat/><ss:Protection/></ss:Style>");
      xls.WriteLine("<ss:Style ss:ID=\"BoldColumn\"><ss:Font x:Family=\"Swiss\" ss:Bold=\"1\"/></ss:Style>");
      xls.WriteLine("<ss:Style ss:ID=\"StringLiteral\"><ss:NumberFormat ss:Format=\"@\"/></ss:Style>");
      xls.WriteLine("<ss:Style ss:ID=\"Decimal\"><ss:NumberFormat ss:Format=\"0.0000\"/></ss:Style>");
      xls.WriteLine("<ss:Style ss:ID=\"Integer\"><ss:NumberFormat ss:Format=\"0\"/></ss:Style>");
      xls.WriteLine("<ss:Style ss:ID=\"DateLiteral\"><ss:NumberFormat ss:Format=\"mm/dd/yyyy;@\"/></ss:Style>");
      xls.WriteLine("</ss:Styles>");
      xls.WriteLine("</ss:Workbook>");
      xls.Flush();
      xls.Close();
    }
  }
  oleCon.Open();
  tableNames = oleCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, param);
}

Dieser code generiert ein weiteres OleDbException Nachricht:

"Externe Tabelle nicht in das erwartete format."

Dass ich sehen kann, OleDbConnection nicht haben eine Methode zum erstellen der Datei, so wie ich ERSTELLEN diese Excel-Datei, so dass ich es verwenden kann?

  • Wenn Sie Zustimmen, nehmen Sie eine andere route gibt es EPPlus
InformationsquelleAutor jp2code | 2012-05-17
Schreibe einen Kommentar