天天看點

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

 /// <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/" 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();

            }

        }

繼續閱讀