Comment changer le format des cellules Excel en utilisant C #

IHi j'essaie de lire le file plat n pour excel. Je peux générer un file excel à l'aide de données, mais les champs de date s'affichent comme #####. J'essaie de changer le format des cellules mais impossible à cela. J'ai ajouté le code à titre de reference. guidez-moi avec gentillesse car j'ai besoin de créer une autre feuille de cette feuille générée avec des formules. La chose la plus drôle à ce sujet est que je vois les dates comme

sur cette fiche, mais si je copy ces données sur une autre feuille, je peux voir les champs de dates au lieu de #####.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.IO; using Microsoft.Office.Interop.Excel; using System.Threading.Tasks; using System.Reflection; namespace report { class Program { static void Main(ssortingng[] args) { ssortingng path = @"flat.txt"; //Flat file System.Data.DataTable table = ReadFile(path); Excel_FromDataTable(table); } private static System.Data.DataTable ReadFile(ssortingng path) { System.Data.DataTable table = new System.Data.DataTable("dataFromFile"); DataColumn colu; for (int i = 0; i < 250; i++) { colu = new DataColumn("", System.Type.GetType("System.Ssortingng")); colu.AllowDBNull = true; table.Columns.Add(colu); } using (StreamReader sr = new StreamReader(path)) { ssortingng line; int rowsCount = 0; while ((line = sr.ReadLine()) != null) { ssortingng[] data = line.Split(new ssortingng[] { "|" },SsortingngSplitOptions.None);// Separated by delimiter | table.Rows.Add(); for (int i = 0; i < data.Length; i++) { //if (data[i].Contains("")) //if (data[i].Equals("")) // table.Rows[rowsCount][i] = "---"; // data[i] = " "; if (!data[i].Equals("")) table.Rows[rowsCount][i] = data[i]; } rowsCount++; } } return table; } private static void Excel_FromDataTable(System.Data.DataTable dt) { //create an excel object and add to a work book.... Application excel = new Application(); //check if you can use ApplicationClass Workbook workbook = excel.Application.Workbooks.Add(true); //add coulmn heading... int iCol = 0; foreach (DataColumn c in dt.Columns) { iCol++; excel.Cells[1, iCol] = c.ColumnName; } //add row int iRow = 0; foreach (DataRow r in dt.Rows) { iRow++; //add each row's cell data... iCol = 0; foreach (DataColumn c in dt.Columns) { iCol++; excel.Cells[iRow + 1, iCol] = r[c.ColumnName]; } } //Globalmissing refernce for objects we are not defining... object missing = System.Reflection.Missing.Value; //excel.get_Range("C3", iRow).NumberFormat = "mm/dd/yyyy"; workbook.SaveAs(@"C:/report.xls", XlFileFormat.xlXMLSpreadsheet, missing, missing, false, false, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing); // If wanting to make Excel visible and activate the worksheet excel.Visible = true; } } } Excel file is like this Column1 Column2 Column3 AAA ######### 103 D-1 17 ######## D-2 17 ######## D-3 17 ######## 

Le champ de la date s'affiche comme ###### car la date est plus longue que la colonne. Essayez de resize les colonnes.

 sheet.Columns.AutoFit(); 

Essayez aussi:

 sheet.Cells[row, column] = Ssortingng.Format("{0:MM/dd/yyyy}", object.DateEntered); 

Réponse mise à jour:

  int iRow = 0; foreach (DataRow r in dt.Rows) { iRow++; //add each row's cell data... iCol = 0; foreach (DataColumn c in dt.Columns) { iCol++; try { DateTime date = Convert.ToDateTime(r[c.ColumnName]); excel.Cells[iRow + 1, iCol] = Ssortingng.Format("{0:MM/dd/yyyy", date); } catch(Exception e) { excel.Cells[iRow + 1, iCol] = r[c.ColumnName]; } } } 

Voici une méthode simple que j'ai écrite qui convertira tout DataTable en CSV

 //Declared at the class Level private const ssortingng tableDelim = "|"; private static DataTable GetDataTabletFromCSVFile(ssortingng csv_file_path) { csvData = new DataTable(defaultTableName); try { using (TextFieldParser csvReader = new TextFieldParser(csv_file_path)) { csvReader.SetDelimiters(new ssortingng[] { //this will be a constant declared at the class level private const ssortingng tableDelim = ","; tableDelim }); csvReader.HasFieldsEnclosedInQuotes = true; ssortingng[] colFields = csvReader.ReadFields(); foreach (ssortingng column in colFields) { DataColumn datecolumn = new DataColumn(column); datecolumn.AllowDBNull = true; csvData.Columns.Add(datecolumn); } while (!csvReader.EndOfData) { ssortingng[] fieldData = csvReader.ReadFields(); //Making empty value as null for (int i = 0; i < fieldData.Length; i++) { if (fieldData[i] == ssortingng.Empty) { fieldData[i] = ssortingng.Empty; //fieldData[i] = null } //Skip rows that have any csv header information or blank rows in them if (fieldData[0].Contains("Disclaimer") || ssortingng.IsNullOrEmpty(fieldData[0])) { continue; } } csvData.Rows.Add(fieldData); } } } catch (Exception ex) { //write your own Exception Messaging here } return csvData; } 

Convertissez le file .CSV et enregistrez-le car le format .XLS est une bonne reference simple de Stackoverflow ainsi que la conversion de ExcelFile .CSV en format .XLS