我现在导出之后,直接就是默认到D盘的,我现在想做成动态的,可以选择路径。
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
System.Data.DataTable datatable = new System.Data.DataTable();
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
try
{
//lz 路径选择
excelApp.Visible = false;
Workbook excelWB = excelApp.Workbooks.Add(System.Type.Missing);
Worksheet excelWS = (Worksheet)excelWB.Worksheets[1];
Range celulas = null;
celulas = excelWS.get_Range("A1", System.Type.Missing);
excelWS.Cells[1, 1] = "";
excelWS.Cells[1, 2] = "控制柜名称";
excelWS.Cells[1, 3] = "控制柜分类";
excelWS.Cells[1, 4] = "时间";
excelWS.Cells[1, 6] = "工作模式";
excelWS.Cells[1, 8] = "报警状态";
excelWS.Cells[1, 10] = "输入电压";
excelWS.Cells[1, 11] = "输出电压";
excelWS.Cells[1, 12] = "输出电流";
excelWS.Cells[1, 13] = "一路";
excelWS.Cells[1, 14] = "二路";
for (int i = 0; i < dsResult.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < dsResult.Tables[0].Columns.Count; j++)
{
celulas.get_Offset(i + 1, j).Cells.Value2 = dsResult.Tables[0].Rows[i][j].ToString();
}
}
excelApp.ActiveWorkbook.RefreshAll();
excelApp.Workbooks.Application.ActiveWorkbook.RefreshAll();
excelWB.SaveAs(@"d:\历史运行数据记录.xls", XlSaveAction.xlSaveChanges, false, false, XlSaveAsAccessMode.xlNoChange);
excelWB.Close(false, null, null);
}
catch (Exception ex)
{
MessageBox.Show("导出失败:" + ex.Message);
}
excelApp.Quit();
MessageBox.Show("导出成功!");
btnExcel.DataContext = "";
HistoryWorkingDataGrid.DataContext = null;
}
求各位大神帮忙改改。。万分感谢啊
你在保存前,先用对话框提示用户选择一个目录,然后用这个目录来SaveAs就行了。
代码里怎么改?
@董毛毛:
SaveFileDialog a = new SaveFileDialog();
a.Filter = "Excel 工作簿 (*.xls)|*.xls";
a.ShowDialog();
excelWB.SaveAs(a.FileName, XlSaveAction.xlSaveChanges, false, false, XlSaveAsAccessMode.xlNoChange);
@Launcher: 谢谢!弄好了,十分感谢呀
@董毛毛: 我也不会。
在saveas前先选择你想要的路径,再保存,如下将选择的路径存在path上面,打开目录选择窗体
using System.Windows.Forms; FolderBrowserDialog myDialog = new FolderBrowserDialog(); myDialog.ShowNewFolderButton = false; myDialog.Description = "选择你要设置的文件夹或目录 "; string path=string.Empty; if (myDialog.ShowDialog() == DialogResult.OK) path= myDialog.SelectedPath; myDialog.Dispose();
谢谢
有什么问题可以进群:58189568,我们可以商量!~~~