• 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;
            }

  • 相关阅读:
    将.net core api 部署成windows服务
    根据2个经纬度点,计算这2个经纬度点之间的距离(通过经度纬度得到距离)
    .NET 基础知识 单文件部署和可执行文件 剪裁独立部署和可执行文件
    通过 InnoSetup 美化安装界面
    拼凑一个ABP VNext管理后台拼凑一个ABP VNext管理后台
    互联网软件的安装包界面设计Inno setup
    weinre  远程实时调试手机上的Web页面 JAVASCRIPT远程调试
    asp.net core web应用以服务的方式安装运行
    用 vue2 和 webpack 快速建构 NW.js 项目
    谷歌插件抓包 similarweb抓包
  • 原文地址:https://www.cnblogs.com/abcdwxc/p/830562.html
Copyright © 2020-2023  润新知