问题描述
asp.net导出excel手动输入文件名怎么做如题
解决方案
解决方案二:
publicstaticvoidExportToExcel(stringfileName,DataGridViewdataGridView1){stringsaveFileName="";//boolfileSaved=false;SaveFileDialogsaveDialog=newSaveFileDialog();saveDialog.DefaultExt="xls";saveDialog.Filter="Excel文件|*.xls";saveDialog.FileName=fileName;saveDialog.ShowDialog();saveFileName=saveDialog.FileName;if(saveFileName.IndexOf(":")<0)return;//被点了取消Microsoft.Office.Interop.Excel.ApplicationxlApp=newMicrosoft.Office.Interop.Excel.Application();if(xlApp==null){MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");return;}Microsoft.Office.Interop.Excel.Workbooksworkbooks=xlApp.Workbooks;Microsoft.Office.Interop.Excel.Workbookworkbook=workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);Microsoft.Office.Interop.Excel.Worksheetworksheet=(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1//写入标题for(inti=0;i<dataGridView1.Columns.Count;i++){worksheet.Cells[1,i+1]=dataGridView1.Columns[i].Name;}//写入数值for(intr=0;r<dataGridView1.Rows.Count-1;r++){for(inti=0;i<dataGridView1.Columns.Count;i++){if(i==1){worksheet.Cells[r+2,i+1]="'"+dataGridView1.Rows[r].Cells[i].Value.ToString();}else{worksheet.Cells[r+2,i+1]=dataGridView1.Rows[r].Cells[i].Value.ToString();}}System.Windows.Forms.Application.DoEvents();}worksheet.Columns.EntireColumn.AutoFit();//列宽自适应if(saveFileName!=""){try{workbook.Saved=true;workbook.SaveCopyAs(saveFileName);//fileSaved=true;}catch(Exceptionex){//fileSaved=false;MessageBox.Show("导出文件时出错,文件可能正被打开!n"+ex.Message);}}xlApp.Quit();GC.Collect();//强行销毁MessageBox.Show(fileName+"保存成功","提示",MessageBoxButtons.OK);}
解决方案三:
引用1楼wc_ling的回复:
publicstaticvoidExportToExcel(stringfileName,DataGridViewdataGridView1){stringsaveFileName="";//boolfileSaved=false;SaveFileDialogsaveDialog=newSaveFileDialog();saveDialog.DefaultExt="xls";saveDialog.Filter="Excel文件|*.xls";saveDialog.FileName=fileName;saveDialog.ShowDialog();saveFileName=saveDialog.FileName;if(saveFileName.IndexOf(":")<0)return;//被点了取消Microsoft.Office.Interop.Excel.ApplicationxlApp=newMicrosoft.Office.Interop.Excel.Application();if(xlApp==null){MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");return;}Microsoft.Office.Interop.Excel.Workbooksworkbooks=xlApp.Workbooks;Microsoft.Office.Interop.Excel.Workbookworkbook=workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);Microsoft.Office.Interop.Excel.Worksheetworksheet=(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1//写入标题for(inti=0;i<dataGridView1.Columns.Count;i++){worksheet.Cells[1,i+1]=dataGridView1.Columns[i].Name;}//写入数值for(intr=0;r<dataGridView1.Rows.Count-1;r++){for(inti=0;i<dataGridView1.Columns.Count;i++){if(i==1){worksheet.Cells[r+2,i+1]="'"+dataGridView1.Rows[r].Cells[i].Value.ToString();}else{worksheet.Cells[r+2,i+1]=dataGridView1.Rows[r].Cells[i].Value.ToString();}}System.Windows.Forms.Application.DoEvents();}worksheet.Columns.EntireColumn.AutoFit();//列宽自适应if(saveFileName!=""){try{workbook.Saved=true;workbook.SaveCopyAs(saveFileName);//fileSaved=true;}catch(Exceptionex){//fileSaved=false;MessageBox.Show("导出文件时出错,文件可能正被打开!n"+ex.Message);}}xlApp.Quit();GC.Collect();//强行销毁MessageBox.Show(fileName+"保存成功","提示",MessageBoxButtons.OK);}
网页上
解决方案四:
用这个SaveFileDialog东东啊
解决方案五:
SaveFileDialog