コード例 #1
0
 private void OpenExelFile(string fileName)
 {
     Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
     if (app == null)
     {
         MessageBox.Show("Excel打开失败!");
         return;
     }
     app.Visible     = false;
     app.UserControl = true;
     Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks;
     Microsoft.Office.Interop.Excel._Workbook workbook  = workbooks.Open(fileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
     app.DisplayAlerts = false;
     try
     {
         app.CopyObjectsWithCells = true;
         Microsoft.Office.Interop.Excel._Worksheet sheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[1];
         sheet1.UsedRange.Copy(Type.Missing);
         AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet spreadsheet = new AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet();
         ((System.ComponentModel.ISupportInitialize)(spreadsheet)).BeginInit();
         this.Controls.Add(spreadsheet);
         ((System.ComponentModel.ISupportInitialize)(spreadsheet)).EndInit();
         spreadsheet.get_Range(this.axSpreadsheet1.Cells[1, 1]).Paste();
     }
     catch (System.Exception exc)
     {
         MessageBox.Show(exc.Message.ToString());
         return;
     }
     app.Quit();
 }
コード例 #2
0
 private void OpenExelFile(string fileName)
 {
     Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
     if (app == null)
     {
         MessageBox.Show("Excel打开失败!");
         return;
     }
     app.Visible = false;
     app.UserControl = true;
     Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks;
     Microsoft.Office.Interop.Excel._Workbook workbook = workbooks.Open(fileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
     app.DisplayAlerts = false;
     try
     {
         app.CopyObjectsWithCells = true;
         Microsoft.Office.Interop.Excel._Worksheet sheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[1];
         sheet1.UsedRange.Copy(Type.Missing);
         AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet spreadsheet = new AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet();
         ((System.ComponentModel.ISupportInitialize)(spreadsheet)).BeginInit();
         this.Controls.Add(spreadsheet);
         ((System.ComponentModel.ISupportInitialize)(spreadsheet)).EndInit();
         spreadsheet.get_Range(this.axSpreadsheet1.Cells[1, 1]).Paste();
     }
     catch (System.Exception exc)
     {
         MessageBox.Show(exc.Message.ToString());
         return;
     }
     app.Quit();
 }