zoukankan      html  css  js  c++  java
  • DataTable,DataGridVIew转换到xls 方法 (转)

            private void dataTableToCsv(DataTable table, string file)
            {
    
                string title = "";
    
                FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
    
                //FileStream fs1 = File.Open(file, FileMode.Open, FileAccess.Read);
    
                StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);
    
                for (int i = 0; i < table.Columns.Count; i++)
                {
    
                    title += table.Columns[i].ColumnName + "	"; //栏位:自动跳到下一单元格
    
                }
    
                title = title.Substring(0, title.Length - 1) + "
    ";
    
                sw.Write(title);
    
                foreach (DataRow row in table.Rows)
                {
    
                    string line = "";
    
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
    
                        line += row[i].ToString().Trim() + "	"; //内容:自动跳到下一单元格
    
                    }
    
                    line = line.Substring(0, line.Length - 1) + "
    ";
    
                    sw.Write(line);
    
                }
    
                sw.Close();
    
                fs.Close();
                MessageBox.Show("数据导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
    
            public void m_mthExport()
            {
                //dataTableToCsv();
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Execl files (*.xls)|*.xls";
                //dlg.Filter = "Excel 2003|*.xls|Excel 2007|*.xlsx";
                dlg.CheckFileExists = false;
                dlg.CheckPathExists = false;
                dlg.FilterIndex = 0;
                dlg.RestoreDirectory = true;
                dlg.CreatePrompt = false;
                dlg.Title = "保存为Excel文件";
    
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    string ss = dlg.FileName;
                    dataTableToCsv(dtSource,dlg.FileName);
                }
               // DataGridViewToExcel(m_objViewer.DgvReportList);
            }
            public void DataGridViewToExcel(DataGridView dgv)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Execl files (*.xls)|*.xls";
                //dlg.Filter = "Excel 2003|*.xls|Excel 2007|*.xlsx";
                dlg.CheckFileExists = false;
                dlg.CheckPathExists = false;
                dlg.FilterIndex = 0;
                dlg.RestoreDirectory = true;
                dlg.CreatePrompt = false;
                dlg.Title = "保存为Excel文件";
    
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Stream myStream;
                    myStream = dlg.OpenFile();
                    StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                    string columnTitle = "";
                    try
                    {
                        //写入列标题  
                        for (int i = 0; i < dgv.ColumnCount; i++)
                        {
                            if (i > 0)
                            {
                                columnTitle += "	";
                            }
                            columnTitle += dgv.Columns[i].HeaderText;
                        }
                        sw.WriteLine(columnTitle);
    
                        //写入列内容  
                        for (int j = 0; j < dgv.Rows.Count; j++)
                        {
                            string columnValue = "";
                            for (int k = 0; k < dgv.Columns.Count; k++)
                            {
                                if (k > 0)
                                {
                                    columnValue += "	";
                                }
                                if (dgv.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                            sw.WriteLine(columnValue);
                        }
                        sw.Close();
                        myStream.Close();
                        MessageBox.Show("数据导出成功!", "提示", MessageBoxButtons.OK);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                    }
                    finally
                    {
                        sw.Close();
                        myStream.Close();
                    }
                }
            }
    

      

  • 相关阅读:
    为什么处理有序数组比无序数组快?
    LeetCode:Longest Common Prefix
    LeetCode:Container With Most Water,Trapping Rain Water
    LeetCode:Substring with Concatenation of All Words (summarize)
    LeetCode:Pow(x, n)
    LeetCode:Combination Sum I II
    LeetCode:N-Queens I II(n皇后问题)
    LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
    LeetCode:Divide Two Integers
    LeetCode:Reverse Nodes in k-Group
  • 原文地址:https://www.cnblogs.com/china-guoch/p/4286718.html
Copyright © 2011-2022 走看看