Servus zusammen.
Ich möchte Daten aus diversen Textboxen in Excel als Logfile ablegen.
Mein Problem ist, dass das Exceldokument nur schreibgeschützt öffnet. Jemand eine Idee?
Berechtigungen für "Jeder" habe ich bereits auf Vollzugriff gesetzt. Andere Formate habe ich auch schon ausprobiert.
private void btnAnmelden_Click(object sender, EventArgs e)
{
tbPersNr.ReadOnly = true;
string Status = "Maschine arbeitsbereit.";
tbStatus.Clear();
this.tbStatus.Text += Status;
if (checkBox5.Checked == true)
{
checkBox6.Enabled = true;
}
checkBox1.Enabled = false;
checkBox2.Enabled = false;
checkBox3.Enabled = false;
checkBox4.Enabled = false;
checkBox5.Enabled = false;
xlexcel = new Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Open("C:\\Users\\Marcel\\Documents\\Visual Studio 2012\\Projects\\Programm Bosch\\Userlog.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorksheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorksheet.Cells[1, 1] = "Name";
xlWorksheet.Cells[1, 2] = "Personalnummer";
Excel.Range usedRange = xlWorksheet.UsedRange;
Excel.Range _lastCell = usedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
int _lastRow = _lastCell.Row;
int _lastCol = _lastCell.Column;
_lastRow++;
int j = 1;
for (int i = _lastRow; i <= _lastRow + 6; i++)
{
xlWorksheet.Cells[_lastRow, 1] = tbNameAnzeige.Text;
xlWorksheet.Cells[_lastRow, 2] = tbPersNr.Text;
j++;
}
}
private void btnAbmelden_Click(object sender, EventArgs e)
{
tbPersNr.ReadOnly = false;
tbPersNr.Clear();
tbStatus.Clear();
this.tbStatus.Text += "Maschine im Wartezustand";
//Uncheck beim Abemelden
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
checkBox5.Checked = false;
checkBox6.Checked = false;
checkBox7.Checked = false;
checkBox8.Checked = false;
checkBox6.Enabled = false;
xlWorkBook.Close(true, misValue, misValue);
xlexcel.Quit();
releaseObject(xlWorksheet);
releaseObject(xlWorkBook);
releaseObject(xlexcel);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Log konnte nicht gespeichert werden" + ex.ToString());
}
finally
{
GC.Collect();
}
}
Hallo Daze1302,
laut https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.open.aspx ist der dritte Parameter das Read-Only. Und das hast du auf "true". Hast das schonmal auf "false" gesetzt?
Gruss
Coffeebean
Microsoft MVP // Me // Blog // GitHub // @Egghead // All my talks // Speakerdeck
Du hast mir den Tag grad gerettet. Ich hab mich wohl verlesen. Vielen lieben Dank!
Das passiert wohl Einigen und darum kann man die Parameter in C# auch benennen, was das wesentlich verständlicher macht
xlWorkbook = xlexcel.Workbooks.Open(
Filename: "C:\\Users\\Marcel\\Documents\\Visual Studio 2012\\Projects\\Programm Bosch\\Userlog.xls",
UpdateLinks: 0,
ReadOnly: true,
Format: 5,
Password: "",
WriteResPassword: "",
IgnoreReadOnlyRecommended: true,
Origin: Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
Delimiter: "\t",
Editable: false,
Notify: false,
Converter: 0,
AddToMru: true,
Local: 1,
CorruptLoad: 0);
Ah, super. Das macht es natürlich einfacher. Ist alles recht undurchsichtig wenn man vor 2 Wochen erst von c++ auf c# umgestiegen ist 😄
Man trennt also in C++ nicht die UI von der Logik? Soso 😉
[Artikel] Drei-Schichten-Architektur
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code