J'apparais pour exceller, mais quand je poste, je trouve que la même ligne a été postée deux fois. donc j'ai 2 lignes. quand je poste de nouveau, il laisse ma première ligne, écrase la deuxième ligne, mais me redonne une copie en tant que troisième ligne.Ajoutant à exceller, mais affiche la même ligne deux fois. C#
Donc dans l'ensemble, je peux poster tout ce dont j'ai besoin et je suis juste parti avec 1 ligne en double. je ne peux pas semble corriger
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
private static Microsoft.Office.Interop.Excel.Application oXL;
private void button1_Click(object sender, EventArgs e)
{
string Combo1 = comboBox1.SelectedItem.ToString();
string path = @"C:\Users\Staff\Desktop\RIT\worksheet.xls";
oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = true;
oXL.DisplayAlerts = false;
mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0,
true, false, false);
mWorkSheets = mWorkBook.Worksheets;
mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;
//I thought issue maybe here..
int colCount = range.Columns.Count;
int rowCount = range.Rows.Count;
for (int index = 1; index < 3; index++)
{
mWSheet1.Cells[rowCount + index, 1] = rowCount + index;
mWSheet1.Cells[rowCount + index, 1] = Combo1 + index;
}
mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
mWSheet1 = null;
mWorkBook = null;
oXL.Quit();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
}
Veuillez essayer 'int index = 2; indice> 0; index - 'à la place => sauf si vous allez retraiter la ligne fraîchement insérée dans l'itération suivante. Cela provoque la double insertion. – cramopy
L'avez vous essayé? Est-ce que mon changement proposé fonctionne? – cramopy
Merci pour votre réponse. Si je mets 0, 1 ou 2, il ne publie rien du tout. la forme s'ouvre toujours et fonctionne. Excel ouvre, sauvegarde et ferme. mais rien n'est publié. –