Ciao a tutti, ho un problema..sto cercando di fare un export dei dati contenuti in una dataTable in un file excel, girando qua e la son riuscito a creare l'algoritmo ma ho un paio di problemi.
Se provo a creare un nuovo foglio contente i dati, è possibile rendere come foglio pre-selezionato quest'ultimo? perchè quando apro il file excel mi trovo il foglio vuoto e non quello "compilato".
Altro punto, se provo a creare il foglio e "compilarlo" non ho problemi e si crea tutto come si deve, se invece provo a inserire su un foglio già esistente non funziona, il codice che uso è questo:
Onestamente, non badate troppo al codice perchè è frutto di prove, riprove e controprove..e magari ve l'ho postato sbagliatocodice://Crezione colonne intestazione string strsql; string workSheetName = "Sheet1"; string separatore = ""; string tipoCella = "varchar(255)"; strsql = "INSERT INTO [" + workSheetName + "] ("; foreach (DataColumn dc in dt.Columns) { strsql = strsql + separatore + dc.ColumnName.Replace(" ", "") + " " + tipoCella; separatore = ","; } strsql = strsql + ")"; OleDbCommand odbCommand = new OleDbCommand(); odbCommand.Connection = _connection; odbCommand.CommandText = strsql; odbCommand.ExecuteNonQuery(); _connection.Close(); headerPresent = HEADER_NO; connectionString = String.Format(EXCEL_CONNECTION_STRING, newPath, headerPresent); _connection = new OleDbConnection(connectionString); _connection.Open(); separatore = ""; strsql = ""; //Creazione righe con i dati esportati foreach (DSGestioneCorsi.VSubscriptionCounterRow row in dt) { strsql = "INSERT INTO [" + workSheetName + "] ("; foreach (DataColumn dc in dt.Columns) { strsql = strsql + separatore + dc.ColumnName.Replace(" ", ""); separatore = ","; } strsql = strsql + ") VALUES( "; separatore = ""; foreach (DataColumn dc in dt.Columns) { strsql = strsql + separatore + "'" + row[dc.ColumnName].ToString() + "'"; separatore = ","; } strsql = strsql + ")"; separatore = ""; odbCommand = new OleDbCommand(strsql, _connection); odbCommand.ExecuteNonQuery(); }![]()
Grazie![]()

Rispondi quotando