コード例 #1
0
        private void AddDocumentNoInLastRow(string filename, string docno)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application ObjExcel     = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbooks   objWorkbooks = ObjExcel.Workbooks;

                Microsoft.Office.Interop.Excel.Workbook book = objWorkbooks.Open(filename, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                Microsoft.Office.Interop.Excel.Sheets    sheets    = book.Worksheets;
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets[1];

                workSheet.Select(Type.Missing);

                Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)workSheet.UsedRange;

                int lastRow = range.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, System.Type.Missing).Row;

                string strRange = string.Format("B{0}", lastRow + 2);

                Microsoft.Office.Interop.Excel.Range docIsoRange = workSheet.get_Range(strRange, Type.Missing);


                //docIsoRange.EntireRow.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp);
                docIsoRange.set_Value(System.Type.Missing, docno);
                //lastCell.EntireRow.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp);
                //range.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(docIsoRange);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);

                book.Save();
                book.Close(false, Type.Missing, Type.Missing);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(book);

                ObjExcel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ObjExcel);
            }
            catch (Exception ex)
            {
                //
            }
        }