|
//保存方式一:保存WorkBook //workbook.SaveAs(@"F:CData.xls", // Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value, // Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value, // Missing.Value,Missing.Value);
//保存方式二:保存WorkSheet // worksheet.SaveAs(@"F:CData2.xls", // Missing.Value, Missing.Value, Missing.Value, Missing.Value, // Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
////保存方式三 //workbook.Saved = true; //workbook.SaveCopyAs(sfd.FileName);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); worksheet = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); workbook = null; workbooks.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks); workbooks = null; excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); excelApp = null; MessageBox.Show("导出Excel完成!"); } }
} private void button4_Click(object sender, EventArgs e) { string strExcelFileName = @"F:\2007-07-16-学生信息.xls"; string strSheetName = "sheet1";
#region Aspnet 操作Excel 正确 ////源的定义 //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + strExcelFileName + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
////Sql语句 //string strExcel = "select * from [" + strSheetName + "$]";
////定义存放的数据表 //DataSet ds = new DataSet();
////连接数据源 //OleDbConnection conn = new OleDbConnection(strConn);
//conn.Open();
////适配到数据源 //OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn); //adapter.Fill(ds,"res");
//conn.Close();
//// 一般的情况下. Excel 表格的第一行是列名 //dataGridView2.DataSource = ds.Tables["res"]; #endregion
#region COM 组件读取复杂Excel Microsoft.Office.Interop.Excel.Application excelApp = null; Microsoft.Office.Interop.Excel.Workbook workBook; Microsoft.Office.Interop.Excel.Worksheet ws = null;
try { excelApp = new Microsoft.Office.Interop.Excel.Application(); workBook = excelApp.Workbooks.Open(@"F:\Book1.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); ws = (Worksheet)workBook.Worksheets[1];
//Excel 默认为 256 列.. MessageBox.Show(ws.Cells.Columns.Count.ToString()); excelApp.Quit(); } catch (Exception ex) { throw ex; } #endregion }
共2页: 上一页 [1] 2 下一页
|