Je le code qui va créer le fichier Excel et la feuille de travail puis insérez les mêmes valeurs.Utiliser Microsoft.Office.Interop pour enregistrer le fichier créé avec C#
Le problème que je suis face que je ne suis pas en mesure d'enregistrer le fichier avec le nom donnant dix colse il.
J'utilisé SaveAs, mais n'a pas fonctionné:
wb.SaveAs(@"C:\mymytest.xlsx", missing, missing, missing, missing,
missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
cette ligne de code me donner cette erreur:
Microsoft Office Excel ne peut pas accéder au fichier 'C: \ A3195000. Il y a plusieurs raisons possibles:
• Le nom du fichier ou le chemin n'existe pas. • Le fichier est utilisé par un autre programme. • Le classeur que vous essayez d'enregistrer porte le même nom qu'un classeur actuellement ouvert.
s'il vous plaît conseiller pour résoudre ce problème.
voici mon code:
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("EXCEL could not be started. Check that your office installation and project references are correct.");
return;
}
xlApp.Visible = true;
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
if (ws == null)
{
MessageBox.Show("Worksheet could not be created. Check that your office installation and project references are correct.");
}
// Select the Excel cells, in the range c1 to c7 in the worksheet.
Range aRange = ws.get_Range("C1", "C7");
if (aRange == null)
{
MessageBox.Show("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
}
// Fill the cells in the C1 to C7 range of the worksheet with the number 6.
Object[] args = new Object[1];
args[0] = 6;
aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);
// Change the cells in the C1 to C7 range of the worksheet to the number 8.
aRange.Value2 = 8;
// object missing = Type.Missing;
// wb.SaveAs(@"C:\mymytest.xlsx", missing, missing, missing, missing,
//missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, //missing);
}
Pourquoi utilisez-vous Type.InvokeMember() pour définir les valeurs des cellules? –
J'ai obtenu cet exemple du site de MSDN ainsi j'essaye de l'employer et emploie Type.InvokeMember() poserais le problème pour sauver le dossier? – Eyla
Non, il semble juste une façon étrange de le faire. –