1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
| using System; using System.Windows.Forms; using System.Xml.Linq; using System.IO; using System.Reflection; using Excel = Microsoft.Office.Interop.Excel;
public string Ziel = @"C:\test.xls"; public string temp = @"C:\Windows\Temp\ablage.xls";
private void Speichern_Click(object sender, EventArgs e) { File.Copy(Ziel, temp); if (File.Exists(Path.Combine(Application.StartupPath, Ziel))) File.Delete(Path.Combine(Application.StartupPath, Ziel)); File.Copy(Path.Combine(Application.StartupPath, temp), Path.Combine(Application.StartupPath, Ziel));
Excel.Application excel = new Excel.ApplicationClass(); excel.Visible = false;
object missing = Missing.Value; string fileName = Path.Combine(Application.StartupPath, Ziel); Excel.Workbook workbook = excel.Workbooks.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets["test"];
int row = 0; Excel.Range range = null; do { row++; range = (Excel.Range)worksheet.Cells[row, 1]; if (range.Value2 == null) break; } while (true);
range = (Excel.Range)worksheet.Cells[row + 1, 1];
range = worksheet.get_Range("A2", missing).get_End(Excel.XlDirection.xlUp); range.EntireRow.Insert(missing, missing); Excel.Range myRangeHeadline = null; myRangeHeadline = worksheet.get_Range("A1", ("H1")); myRangeHeadline.Font.Bold = true; myRangeHeadline.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; myRangeHeadline.Borders.Weight = Excel.XlBorderWeight.xlThick;
worksheet.Cells[2, 1] = Datum.Text; worksheet.Cells[2, 2] = Zeit.Text; worksheet.Cells[2, 3] = 1_com.Text.ToString(); worksheet.Cells[2, 4] = 2.Text + "ml"; worksheet.Cells[2, 5] = pi; worksheet.Cells[2, 6] = Ka; worksheet.Cells[2, 7] = kon_com.Text.ToString(); worksheet.Cells[2, 8] = Tro;
worksheet.Cells[1, 1] = "Datum"; worksheet.Cells[1, 2] = "Zeit"; worksheet.Cells[1, 3] = "Tri"; worksheet.Cells[1, 4] = "Me"; worksheet.Cells[1, 5] = "Pi"; worksheet.Cells[1, 6] = "Ka"; worksheet.Cells[1, 7] = "Kon"; worksheet.Cells[1, 8] = "Tro";
workbook.Save(); File.Delete(Path.Combine(Application.StartupPath, temp)); File.Copy(Path.Combine(Application.StartupPath, Ziel), Path.Combine(Application.StartupPath, temp)); excel.Quit(); File.Delete(temp); } |