大家帮帮忙啊!100分求用模板导出Excel之后如何能让用户选择保存路径,而不是用写死的路径,,在线等待!!!!!!!!!
如果把导出的路径写死的话 可以导出来,但是不符合用户的需求啊!!
大家帮帮我 谢谢
代码::
public class ExcelHelper
{
protected string templetFile = null;
protected string outputFile = null;
protected object missing = Missing.Value;
/// <summary>
/// 构造函数,需指定模板文件和输出文件完整路径
/// </summary>
/// <param name="templetFilePath">Excel模板文件路径</param>
/// <param name="outputFilePath">输出Excel文件路径</param>
public ExcelHelper(string templetFilePath, string outputFilePath)
{
if (templetFilePath == null)
throw new Exception("Excel模板文件路径不能为空!");
if (outputFilePath == null)
throw new Exception("输出Excel文件路径不能为空!");
if (!File.Exists(templetFilePath))
throw new Exception("指定路径的Excel模板文件不存在!");
this.templetFile = templetFilePath;
this.outputFile = outputFilePath;
}
/// <summary>
/// 将DataTable数据写入Excel文件(套用模板并分页)
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="rows">每个WorkSheet写入多少行数据</param>
/// <param name="top">行索引</param>
/// <param name="left">列索引</param>
/// <param name="sheetPrefixName">WorkSheet前缀名,比如:前缀名为“Sheet”,那么WorkSheet名称依次为“Sheet-1,Sheet-2...”</param>
public void DataTableToExcel(DataTable dt, int top, int left, string sheetPrefixName)
{
int rowCount = dt.Rows.Count; //源DataTable行数
int colCount = dt.Columns.Count; //源DataTable列数
DateTime beforeTime;
DateTime afterTime;
if (sheetPrefixName == null || sheetPrefixName.Trim() == "")
sheetPrefixName = "Sheet";
//创建一个Application对象并使其可见
beforeTime = DateTime.Now;
Excel.Application app = new Excel.ApplicationClass();
app.Visible = true;
afterTime = DateTime.Now;
//打开模板文件,得到WorkBook对象
Excel.Workbook workBook = app.Workbooks._Open(templetFile, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing);
//得到WorkSheet对象
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
#region 将源DataTable数据写入Excel
//获取要写入数据的WorkSheet对象,并重命名
Excel.Worksheet sheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1);
sheet.Name = sheetPrefixName + "-" + 1;
//将dt中的数据写入WorkSheet
for (int j = 0; j < rowCount; j++)
{
for (int k = 0; k < colCount-1; k++)
{
sheet.Cells[top + j, left + k] = dt.Rows[j][k+1].ToString();
}
}
//NAR(sheet);
//写文本框数据
Excel.TextBox txtDate = (Excel.TextBox)sheet.TextBoxes("txtDate");
Excel.TextBox txtVersion = (Excel.TextBox)sheet.TextBoxes("txtVersion");
txtVersion.Text = DateTime.Now.ToString("yyyy");
txtDate.Text = DateTime.Now.ToString("MM");
#endregion
//输出Excel文件并退出
try
{
workBook._SaveAs(outputFile, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing); --outputFile就是写死的导出的路径!!!!!!!!!!!!!!!
workBook.Close(null, null, null);
app.Workbooks.Close();
app.Application.Quit();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
workSheet = null;
workBook = null;
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch (Exception e)
{
throw e;
}
finally
{
Process[] myProcesses;
DateTime startTime;
myProcesses = Process.GetProcessesByName("Excel");
//得不到Excel进程ID,暂时只能判断进程启动时间
//foreach (Process myProcess in myProcesses)
//{
// startTime = myProcess.StartTime;
// if (startTime > beforeTime && startTime < afterTime)
// {
// myProcess.Kill();
// }
//}
}
}