C# Stream流方式导入Excel,htm,txt,Doc

来源:互联网 发布:网络剧推荐 编辑:程序博客网 时间:2024/06/05 07:15

 /// <summary>
        /// 流方式导出Excel
        /// </summary>
        /// <param name="dgv"></param>
        private void ExportToExcel(DataGridView dgv)
        {
            if (dgv.Rows.Count == 0)
            {
                MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            // 保存对话框
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl文件 (*.xls)|*.xls|Xml文件 (*.xml)|*.xml|网页文件 (*.htm)|*.htm|网页文件 (*.html)|*.html|Doc文件 (*.doc)|*.doc|文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "导出文件保存路径";//为Excel
            saveFileDialog.FileName = null;
            saveFileDialog.ShowDialog();

            string Path = saveFileDialog.FileName;
            //被点了"取消"
            if (Path.IndexOf(":") < 0)
            {
                return;
            }

            //获得数据类型
            string type = Path.Substring(Path.IndexOf(".") + 1);
            if (type.Equals("doc", StringComparison.CurrentCultureIgnoreCase))
            {
                //object path = Path;
                //Object none = System.Reflection.Missing.Value;
                //try
                //{
                //    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                //    Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
                //    //建立表格
                //    Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, dgv.Rows.Count + 1, dgv.Columns.Count, ref none, ref none);

                //    try
                //    {
                //        for (int i = 0; i < dgv.Columns.Count; i++)//设置标题
                //        {
                //            table.Cell(1, i + 1).Range.Text = dgv.Columns[i].HeaderText;
                //        }

                //        for (int i = 0; i < dgv.Rows.Count; i++)//填充数据
                //        {
                //            for (int j = 0; j < dgv.Columns.Count; j++)
                //            {
                //                table.Cell(i + 2, j + 1).Range.Text = dgv[j, i].Value.ToString();
                //            }
                //        }
                //        document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
                //        document.Close(ref none, ref none, ref none);
                //        MessageBox.Show("数据已经成功导出到:" + Path, "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    }
                //    catch (Exception ex)
                //    {
                //        MessageBox.Show("文件 " + Path + " 正由另一起程使用,/r/n请先关闭该进程!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //        return;
                //    }
                //    finally
                //    {
                //        wordApp.Quit(ref none, ref none, ref none);
                //    }
                //    return;
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    return;
                //}
            }

            Stream myStream;
            try
            {
                myStream = saveFileDialog.OpenFile();
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件 " + Path + " 正由另一起程使用,/r/n请先关闭该进程!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            string columnTitle = "";

            if (type.Equals("htm", StringComparison.CurrentCultureIgnoreCase) || type.Equals("html", StringComparison.CurrentCultureIgnoreCase))
            {
                //写入列标题
                columnTitle += "<table align=/"center/" border=/"1px/"><tr>";
                for (int i = 0; i < dgv.ColumnCount; i++)
                {
                    columnTitle += "<td>";
                    columnTitle += dgv.Columns[i].HeaderText + "</td>";
                }

                //写入列内容
                for (int j = 0; j < dgv.Rows.Count; j++)
                {
                    columnTitle += "</tr><tr>";
                    for (int k = 0; k < dgv.Columns.Count; k++)
                    {
                        columnTitle += "<td>";
                        if (dgv.Rows[j].Cells[k].Value == null)
                            columnTitle += "&nbsp;</td>";
                        else
                            columnTitle += dgv.Rows[j].Cells[k].Value.ToString().Trim() + "</td>";
                    }
                }
                columnTitle += "</tr></table>";
                try
                {
                    sw.WriteLine(columnTitle);
                }
                catch
                {
                    MessageBox.Show("文件 " + Path + " 正由另一起程使用,/r/n请先关闭该进程!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
                MessageBox.Show("数据已经成功导出到:" + Path, "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try
            {
                //写入列标题
                for (int i = 0; i < dgv.ColumnCount; i++)
                {
                    if (i > 0)
                    {
                        columnTitle += "/t";
                    }
                    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 += "/t";
                        }
                        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("数据已经成功导出到:" + Path, "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                sw.Close();
                myStream.Close();
            }
        }