• C#在FORM页面上将excel表格从SQL数据库导出,导入txt格式表格


      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using Excel = Microsoft.Office.Interop.Excel;
     10 using System.Data.SqlClient;
     11 
     12 namespace FormPhoneWork
     13 {
     14     public partial class takeinto : Form
     15     {
     16         public takeinto()
     17         {
     18             InitializeComponent();
     19 
     20         }
     21 
     22         private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
     23         {
     24 
     25         }
     26 
     27         public int DataTabletoExcel(DataGridView tmpDataTable)
     28         {
     29             if (tmpDataTable.RowCount == 0)
     30                 return 1;
     31             try
     32             {
     33                 saveFileDialog1.Filter = "Execl files (*.xlsx)|*.xlsx";
     34                 saveFileDialog1.FilterIndex = 0;
     35                 saveFileDialog1.RestoreDirectory = true;
     36                 saveFileDialog1.Title = "导出文件保存路径";
     37                 saveFileDialog1.FileName = null;
     38                 saveFileDialog1.ShowDialog();
     39                 string FileName = saveFileDialog1.FileName;
     40 
     41                 if (FileName != "")
     42                 {
     43 
     44                     int rowNum = tmpDataTable.Rows.Count;
     45 
     46                     int columnNum = tmpDataTable.Columns.Count;
     47                     int rowIndex = 1;
     48                     int columnIndex = 0;
     49 
     50                     Excel.Application xlApp = new Excel.Application();
     51                     xlApp.DefaultFilePath = "";
     52                     xlApp.DisplayAlerts = true;
     53                     xlApp.SheetsInNewWorkbook = 1;
     54                     Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
     55 
     56                     foreach (DataGridViewColumn dc in tmpDataTable.Columns)
     57                     {
     58                         columnIndex++;
     59 
     60                         xlApp.Cells[rowIndex, columnIndex] = dc.HeaderText;
     61                     }
     62 
     63                     for (int i = 0; i < rowNum; i++)
     64                     {
     65 
     66                         rowIndex++;
     67                         columnIndex = 0;
     68                         for (int j = 0; j < columnNum; j++)
     69                         {
     70                             columnIndex++;
     71                             xlApp.Cells[rowIndex, columnIndex] = tmpDataTable[j, i].Value;
     72                         }
     73 
     74                     }
     75                     xlBook.SaveCopyAs(FileName);
     76                     MessageBox.Show("数据已经成功导出到:" + saveFileDialog1.FileName.ToString(),
     77                         "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
     78 
     79                 }
     80                 return 0;
     81             }
     82             catch (System.Exception)
     83             {
     84                 return 2;
     85             }
     86         }
     87 
     88         private void button1_Click(object sender, EventArgs e)
     89         {
     90             string str = "select * from " + textBox1.Text;
     91             string source = "server=(local);" + "integrated security=SSPI;" + "database=PhoneWork";
     92             SqlConnection conn = new SqlConnection(source);
     93             DataSet ds = new DataSet();
     94             SqlDataAdapter da = new SqlDataAdapter(str, source);
     95             da.Fill(ds, textBox1.Text);
     96             dataGridView1.AutoGenerateColumns = true;
     97             dataGridView1.DataSource = ds;
     98             dataGridView1.DataMember = textBox1.Text;
     99             DataTabletoExcel(dataGridView1);
    100         }
    101 
    102         private void textBox1_TextChanged(object sender, EventArgs e)
    103         {
    104 
    105         }
    106 
    107         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    108         {
    109 
    110         }
    111 
    112 
    113 
    114         private void button2_Click(object sender, EventArgs e)
    115         {
    116             openFileDialog1.ShowDialog();
    117             DataExceltoTable(dataGridView1);
    118 
    119 
    120         }
    121         public void DataExceltoTable(DataGridView tmpDataTable)
    122         {
    123             string source = "server=(local);" + "integrated security=SSPI;" + "database=PhoneWork";
    124            // string str = "truncate table " + textBox1.Text;//清空原来的表格
    125             SqlConnection conn = new SqlConnection(source);
    126             conn.Open();
    127             try
    128             {
    129                // SqlCommand card = new SqlCommand(str, conn);
    130                 //card.ExecuteNonQuery();
    131                 SqlCommand com = new SqlCommand("BULK INSERT PhoneWork.dbo."+ textBox1.Text + " FROM '" + openFileDialog1.FileName +
    132                     "'WITH  (FIELDTERMINATOR = ',',ROWTERMINATOR= '\n')", conn);
    133                 com.ExecuteNonQuery();
    134                 MessageBox.Show("导入数据成功");
    135 
    136             }
    137 
    138             catch(Exception e)
    139             {
    140                 MessageBox.Show(e.ToString());
    141             }
    142         }
    143     }
    144 }
  • 相关阅读:
    参考vue.js实现双向绑定的方法理解双向绑定原理(:Object.defineProperty和发布-订阅模式)
    不错的站点 博文
    使用C#动态生成Word文档/Excel文档的程序测试通过后,部署到IIS服务器上,不能正常使用的问题解决方案
    详解HTML<head> 头标签元素的意义以及使用场景
    css奇特用法之 IMG添加背景图片配合显示--效果惊艳
    .net面试题-15k+左右
    微信小程序IOS真机调试发生了SSL 错误,无法建立与该服务器的安全连接
    微信小程序自定义组件-下拉框
    微信小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以
    微信小程序循环中点击一个元素,其他的元素不发生变化,类似点击一个循环中的语音,其他的不发生点击事件
  • 原文地址:https://www.cnblogs.com/zhangyunyun/p/2915770.html
Copyright © 2020-2023  润新知