欢迎来到天天文库
浏览记录
ID:38861352
大小:19.01 KB
页数:3页
时间:2019-06-20
《C#导入、导出Excel的代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingMicrosoft.Office.Interop.Excel;namespaceExcelTools{publicclassReadingAndwriting{//////导出Excel//////da
2、taGridView控件名///路径名publicstaticvoidWritingExcel(DataGridViewgridView,stringfilename){Microsoft.Office.Interop.Excel.Applicationapp=newMicrosoft.Office.Interop.Excel.Application();Workbookbook=app.Workbooks.Add(Typ
3、e.Missing);Worksheetsheet=book.Worksheets[1]asWorksheet;//sheet.Name="student";//遍历所有的列标题for(inti=0;i4、olumns.Count;j++){sheet.Cells[i+2,j+1]=gridView.Rows[i].Cells[j].Value;}}//设置禁止弹出保存和覆盖的询问提示框app.DisplayAlerts=false;app.AlertBeforeOverwriting=false;//sheet.SaveAs(Environment.CurrentDirectory+"\"+filename+".xls",Type.Missing,Type.Missing,Type.Missin5、g,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);//保存excel文件sheet.SaveAs(filename,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);book.Close(Type.Mis6、sing,Type.Missing,Type.Missing);app.Workbooks.Close();app.Quit();}//////导入Excel//////dataGridView控件名///路径名publicstaticvoidReadingExcel(DataGridViewgridView,stringfileName){7、Microsoft.Office.Interop.Excel.Applicationapp=newMicrosoft.Office.Interop.Excel.Application();Workbookbook=app.Workbooks.Open(fileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing8、,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);Worksheetsheet=book.Worksheets[1]asWorksheet;gridView.Rows.Clear();gridView.Columns.Clear();//添加每一列的列标题for(inti=1;;i++){stringvalue=(sheet.Cells[1,i]asRange).Text.ToString();//
4、olumns.Count;j++){sheet.Cells[i+2,j+1]=gridView.Rows[i].Cells[j].Value;}}//设置禁止弹出保存和覆盖的询问提示框app.DisplayAlerts=false;app.AlertBeforeOverwriting=false;//sheet.SaveAs(Environment.CurrentDirectory+"\"+filename+".xls",Type.Missing,Type.Missing,Type.Missin
5、g,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);//保存excel文件sheet.SaveAs(filename,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);book.Close(Type.Mis
6、sing,Type.Missing,Type.Missing);app.Workbooks.Close();app.Quit();}//////导入Excel//////dataGridView控件名///路径名publicstaticvoidReadingExcel(DataGridViewgridView,stringfileName){
7、Microsoft.Office.Interop.Excel.Applicationapp=newMicrosoft.Office.Interop.Excel.Application();Workbookbook=app.Workbooks.Open(fileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing
8、,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);Worksheetsheet=book.Worksheets[1]asWorksheet;gridView.Rows.Clear();gridView.Columns.Clear();//添加每一列的列标题for(inti=1;;i++){stringvalue=(sheet.Cells[1,i]asRange).Text.ToString();//
此文档下载收益归作者所有