• XML,Excel导入导出总结


    xml 导入
            public static System.Data.DataTable GetXmlTable(string fileName)
            {
                System.Data.DataSet dataSetXml;
                System.Data.DataTable dataTableXml;          
                dataSetXml = new DataSet();
                dataTableXml = new System.Data.DataTable();
                try
                {
                    dataSetXml.ReadXml(fileName);
                    dataTableXml = dataSetXml.Tables[1];          

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                return dataTableXml;
            }
    xml导出
        protected void ReadDataSetToXML(DataSet ds, string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            XmlTextWriter xtw = new XmlTextWriter(fs, System.Text.Encoding.UTF8);
            ds.WriteXml(xtw, XmlWriteMode.IgnoreSchema);
            xtw.Close();
            fs.Close();
        }
    Excel导入
            public static System.Data.DataTable GetExcelTable(string fileName, int dataStartLine, int nameLine, int columnCount)
            {
                System.Data.DataTable dtRet = null;
                DataRow dr;
                Application app = null;
                Workbook wb = null;
                Worksheet ws;
                Range range;
                object objMiss = null;
                int iRow;
                StringBuilder sbTemp;
                string sTemp = string.Empty;

                Console.WriteLine(DateTime.Now.ToLongTimeString() + " 正在读取Excel数据");
                objMiss = System.Reflection.Missing.Value;
                try
                {
                    sbTemp = new StringBuilder();
                    dtRet = new System.Data.DataTable();
                    app = new Application();
                    app.DisplayAlerts = false;
                    app.Visible = false;
                    wb = app.Workbooks.Open(fileName, 1, true, objMiss, objMiss, objMiss, objMiss,
                        objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss);
                    ws = (Worksheet)wb.Sheets[1];

                    for (int i = 1; i <= columnCount; i++)
                    {
                        range = (Range)ws.get_Range(num2Char(i) + nameLine, objMiss);
                        if (range.Formula.ToString().Length > 0)
                        {
                            dtRet.Columns.Add(range.Formula.ToString().Trim(), typeof(string));
                        }
                        else
                        {
                            columnCount--;
                            i--;
                        }
                    }

                    iRow = dataStartLine;
                    while (true)
                    {
                        dr = dtRet.NewRow();
                        sbTemp.Length = 0;
                        for (int i = 1; i <= columnCount; i++)
                        {
                            range = (Range)ws.get_Range(num2Char(i) + iRow, objMiss);
                            try
                            {
                                sTemp = range.Formula.ToString();
                            }
                            catch (System.Runtime.InteropServices.COMException)
                            {
                                sTemp = string.Empty;
                            }
                            sbTemp.Append(sTemp);
                            dr[i - 1] = sTemp;
                        }
                        if (sbTemp.Length > 0)
                        {
                            dtRet.Rows.Add(dr);
                            iRow++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    wb.Close(false, objMiss, objMiss);
                    app.Quit();
                    app = null;
                }
                Console.WriteLine(DateTime.Now.ToLongTimeString() + " Excel数据读取完毕");

                return dtRet;
            }
    Excel导出
        protected void ExportOfExcel()
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=Publisher.xls");
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            // Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");如果设置为 GetEncoding("GB2312"),导出的文件将会出现乱码。

            //设置输出文件类型为excel文件。
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.GridView1.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();
        }
    Access,FoxPro等导入
         private static System.Data.DataTable getOleDbTable(string tableName, string fileName, string connString)
            {
                OleDbDataAdapter oda;
                string sql;
                System.Data.DataTable dtRet = new System.Data.DataTable();

                sql = "select * from " + tableName;
                oda = new OleDbDataAdapter(sql, connString);
                oda.Fill(dtRet);

                return dtRet;
            }

  • 相关阅读:
    oracle 执行 delete user$ 误删所有用户信息后的数据恢复流程
    FTP地址格式如下:“ftp://用户名:密码@FTP服务器IP”
    oracle 单列索引 多列索引的性能测试
    浅谈Java中的引用
    JVM内存结构---《深入理解Java虚拟机》学习总结
    HashSet的故事----Jdk源码解读
    为什么这些java接口没有抽象方法?浅谈Java标记接口
    随手编程---快速排序(QuickSort)-Java实现
    java 关键字 assert的学习
    Timer的故事----Jdk源码解读
  • 原文地址:https://www.cnblogs.com/abcdwxc/p/830562.html
Copyright © 2020-2023  润新知