关于数据导出到excel的问题。。。。。。。。。。。。。。。。。。。。。。。。。

时间:2021-02-06 09:21:11
string FileName="c:\\address.xls";

long totalCount=dt.Rows.Count;
long rowRead=0;
float percent=0;
Excel.Application xlApp=null;
xlApp=new Excel.Application();
Excel.Workbooks workbooks=xlApp.Workbooks;
Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Excel.Range range;
worksheet.Cells[1,1]="姓名";
worksheet.Cells[1,2]="手机号码";
worksheet.Cells[1,3]="电子邮件";
worksheet.Cells[1,4]="备用电子邮件";
worksheet.Cells[1,5]="家里电话";
worksheet.Cells[1,6]="公司电话";
worksheet.Cells[1,7]="传真";
worksheet.Cells[1,8]="家里地址";
worksheet.Cells[1,9]="家里的邮政编码";
worksheet.Cells[1,10]="个人主页";
for(int r=0;r<dt.Rows.Count;r++)
{
DataRow dr = dt.Rows[r];
worksheet.Cells[r+2,1]=dr["AB_Name"].ToString();
worksheet.Cells[r+2,2]=dr["AB_Email"].ToString();
worksheet.Cells[r+2,3]=dr["AB_TelNum"].ToString();
worksheet.Cells[r+2,4]="";
worksheet.Cells[r+2,5]="";
worksheet.Cells[r+2,6]=dr["AB_HomeTel"].ToString();
worksheet.Cells[r+2,7]="";
worksheet.Cells[r+2,8]="";
worksheet.Cells[r+2,9]="";
worksheet.Cells[r+2,10]="";
rowRead++;
percent=((float)(100*rowRead))/totalCount;   
}
range=worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[dt.Rows.Count+2,dt.Columns.Count]);
workbook.Saved =true;
workbook.SaveCopyAs(FileName);


这段代码是直接将导出的excel的文件放在c盘里的,

我要做成出现一个另存为的对话框,让别人可以选择存放路径的。该怎样改呢?

2 个解决方案

#1


代码怎么一放上去就乱了?再发过。


string FileName="c:\\address.xls";
long totalCount=dt.Rows.Count;
long rowRead=0;
float percent=0;
Excel.Application xlApp=null;
xlApp=new Excel.Application();
Excel.Workbooks workbooks=xlApp.Workbooks;
Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Excel.Range range;
worksheet.Cells[1,1]="姓名";
worksheet.Cells[1,2]="手机号码";
worksheet.Cells[1,3]="电子邮件";
worksheet.Cells[1,4]="备用电子邮件";
worksheet.Cells[1,5]="家里电话";
worksheet.Cells[1,6]="公司电话";
worksheet.Cells[1,7]="传真";
worksheet.Cells[1,8]="家里地址";
worksheet.Cells[1,9]="家里的邮政编码";
worksheet.Cells[1,10]="个人主页";
for(int r=0;r<dt.Rows.Count;r++)
{
DataRow dr = dt.Rows[r];
worksheet.Cells[r+2,1]=dr["AB_Name"].ToString();
worksheet.Cells[r+2,2]=dr["AB_Email"].ToString();
worksheet.Cells[r+2,3]=dr["AB_TelNum"].ToString();
worksheet.Cells[r+2,4]="";
worksheet.Cells[r+2,5]="";
worksheet.Cells[r+2,6]=dr["AB_HomeTel"].ToString();
worksheet.Cells[r+2,7]="";
worksheet.Cells[r+2,8]="";
worksheet.Cells[r+2,9]="";
worksheet.Cells[r+2,10]="";
rowRead++;
percent=((float)(100*rowRead))/totalCount;   
}
range=worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[dt.Rows.Count+2,dt.Columns.Count]);
workbook.Saved =true;
workbook.SaveCopyAs(FileName);

#2


加个SaveFileDialog控件就可以了

#1


代码怎么一放上去就乱了?再发过。


string FileName="c:\\address.xls";
long totalCount=dt.Rows.Count;
long rowRead=0;
float percent=0;
Excel.Application xlApp=null;
xlApp=new Excel.Application();
Excel.Workbooks workbooks=xlApp.Workbooks;
Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Excel.Range range;
worksheet.Cells[1,1]="姓名";
worksheet.Cells[1,2]="手机号码";
worksheet.Cells[1,3]="电子邮件";
worksheet.Cells[1,4]="备用电子邮件";
worksheet.Cells[1,5]="家里电话";
worksheet.Cells[1,6]="公司电话";
worksheet.Cells[1,7]="传真";
worksheet.Cells[1,8]="家里地址";
worksheet.Cells[1,9]="家里的邮政编码";
worksheet.Cells[1,10]="个人主页";
for(int r=0;r<dt.Rows.Count;r++)
{
DataRow dr = dt.Rows[r];
worksheet.Cells[r+2,1]=dr["AB_Name"].ToString();
worksheet.Cells[r+2,2]=dr["AB_Email"].ToString();
worksheet.Cells[r+2,3]=dr["AB_TelNum"].ToString();
worksheet.Cells[r+2,4]="";
worksheet.Cells[r+2,5]="";
worksheet.Cells[r+2,6]=dr["AB_HomeTel"].ToString();
worksheet.Cells[r+2,7]="";
worksheet.Cells[r+2,8]="";
worksheet.Cells[r+2,9]="";
worksheet.Cells[r+2,10]="";
rowRead++;
percent=((float)(100*rowRead))/totalCount;   
}
range=worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[dt.Rows.Count+2,dt.Columns.Count]);
workbook.Saved =true;
workbook.SaveCopyAs(FileName);

#2


加个SaveFileDialog控件就可以了