zoukankan      html  css  js  c++  java
  • wpf导入Excel

    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

    try
    {
    excelApp.Visible = false;

    //Cria Planilha
    Workbook excelWB = excelApp.Workbooks.Add(System.Type.Missing);

    //Cria Worksheet
    Worksheet excelWS = (Worksheet)excelWB.Worksheets[1];

    Range celulas = null;
    celulas = excelWS.get_Range("A1", System.Type.Missing);

    //La?o para ler todas as linhas e colunas do grid
    for (int i = 0; i < ((System.Data.DataTable)(dataGrid2.DataContext)).Rows.Count; i++)
    {
    for (int j = 0; j < ((System.Data.DataTable)(dataGrid2.DataContext)).Columns.Count; j++)
    {
    //Insere valores das células do grid na célula do excel
    celulas.get_Offset(i, j).Cells.Value2 = ((System.Data.DataTable)(dataGrid2.DataContext)).Rows[i][j].ToString();
    }
    }

    excelApp.ActiveWorkbook.RefreshAll();
    excelApp.Workbooks.Application.ActiveWorkbook.RefreshAll();

    //Salva Arquivo
    excelWB.SaveAs(txtArmazenagem.Text + "\\" + txtNomeArquivoArmazenagem.Text + ".xls", XlSaveAction.xlSaveChanges, System.Type.Missing, System.Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
    excelWB.Close(false, null, null);
    }
    catch (Exception ex)
    {
    MessageBoxResult result = MessageBox.Show(ex.Message);
    //throw ex;
    }
    finally
    {
    excelApp.Quit();
    MessageBoxResult result = MessageBox.Show("Exporta??o concluida com sucesso.");
    txtArmazenagem.Text = "";
    txtNomeArquivoArmazenagem.Text = "";
    dataGrid2.DataContext = null;
    }

  • 相关阅读:
    校园活动管理-毕业设计
    Golang时间字符串转换计算器
    5.21 Go秘籍:异步收割,永葆单身
    Golang东北之旅—channel
    Python3下载豆瓣音乐人小站音乐
    一张图记住4种树
    人生如锁
    Golang构造二叉树解决整数排序问题
    用Golang单元测试写作文
    Golang定时任务表达式合法性校验
  • 原文地址:https://www.cnblogs.com/Utionsoft/p/2716025.html
Copyright © 2011-2022 走看看