C#读Excel 验证码 读大文件

    #region Excel
    public class Excel
    {
        private string _xlsPath;
        public Excel()
        {
        }
        public DataSet ExcelToDs(string XlsPath)
        {
           // string xlsPath = Server.MapPath("~/app_data/somefile.xls"); // 绝对物理路径
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data source=" + XlsPath;

            OleDbConnection conn = new OleDbConnection(connStr);
            conn.Open();
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            string[] strTableNames = new string[dtSheetName.Rows.Count];
            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
            }
            // 查询语句
           string sql = "SELECT * FROM [" + strTableNames[0] + "]";
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr);
            da.Fill(ds);    // 填充DataSet       
            return ds;
            /*
             *  
            //包含excel中表名的字符串数组


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jackyxu_2008/archive/2009/04/26/4126264.aspx
    */
            /*
             * 读Excel2007
             * OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetname + "$]", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";" + "Extended Properties=/"Excel 12.0;HDR=NO;IMEX=1;/""); DataSet ds = new DataSet(); adapter.Fill(ds);
             */
        }

        public DataSet ExcelToDs(string Cols,string XlsPath)
        {
            // string xlsPath = Server.MapPath("~/app_data/somefile.xls"); // 绝对物理路径
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data source=" + XlsPath;

            OleDbConnection conn = new OleDbConnection(connStr);
            conn.Open();
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            string[] strTableNames = new string[dtSheetName.Rows.Count];
            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
            }
            conn.Dispose();
            // 查询语句
            //string sql = "(SELECT " + Cols + " FROM [Sheet1$]) union (SELECT " + Cols + " FROM [Sheet2$])  ";

            DataSet ds = new DataSet();
            OleDbDataAdapter da;
            //for (int i = 0; i < strTableNames.Length; i++)
            //{
            //    string sql = "SELECT " + Cols + "  FROM [" + strTableNames[i] + "]";
            //    da = new OleDbDataAdapter(sql, connStr);
            //    da.Fill(ds,"out");    // 填充DataSet
            //}
            string sql = "SELECT " + Cols + "  FROM [" + strTableNames[0] + "]";
            da = new OleDbDataAdapter(sql, connStr);
            da.Fill(ds);    // 填充DataSet
            da.Dispose();

            return ds;

        }
    }
    #endregion
    #region 验证码
    public class ValidateCode
    {
        private int _int_NumberLength;
        private string _str_ValidateCode;
        public ValidateCode()
        {
        }
    public string GetRandomNumberString(int int_NumberLength)
    {
        string str_Number = string.Empty;
        Random theRandomNumber = new Random();

        for (int int_index = 0; int_index < int_NumberLength; int_index++)
            str_Number += theRandomNumber.Next(10).ToString();

        return str_Number;
    }
    public Color GetRandomColor()
    {
        int number;
        char code;
        string checkCode = String.Empty;
        Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
        //  对于C#的随机数,没什么好说的
        System.Threading.Thread.Sleep(RandomNum_First.Next(50));
        Random RandomNum_Sencond = new Random();
        for (int i = 0; i < 5; i++)
        {
            number = RandomNum_Sencond.Next();
            if (number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else
                code = (char)('A' + (char)(number % 26));

            checkCode += code.ToString();
        }
        //  为了在白色背景上显示,尽量生成深色
        int int_Red = RandomNum_First.Next(256);
        int int_Green = RandomNum_Sencond.Next(256);
        int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
        int_Blue = (int_Blue > 255) ? 255 : int_Blue;
        return Color.FromArgb(int_Red, int_Green, int_Blue);
    }

        public void CreateImage(HttpResponse Response,string str_ValidateCode)
        {
            int int_ImageWidth = str_ValidateCode.Length * 13;
            Random newRandom = new Random();
            //  图高20px
            Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);
            Graphics theGraphics = Graphics.FromImage(theBitmap);
            //  白色背景
            theGraphics.Clear(Color.White);
            //  灰色边框
            theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);

            //  10pt的字体
            Font theFont = new Font("Arial", 10);

            for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
            {
                string str_char = str_ValidateCode.Substring(int_index, 1);
                //Brush newBrush = new SolidBrush(GetRandomColor());
                System.Drawing.Drawing2D.LinearGradientBrush newBrush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, theBitmap.Width, theBitmap.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
                theGraphics.DrawString(str_char, theFont, newBrush, thePos);
            }


            //画图片的前景噪音点
            for (int i = 0; i < 100; i++)
            {
                int x = newRandom.Next(theBitmap.Width);
                int y = newRandom.Next(theBitmap.Height);
                theBitmap.SetPixel(x, y, Color.FromArgb(newRandom.Next()));
            }

            //  将生成的图片发回客户端
            MemoryStream ms = new MemoryStream();
            theBitmap.Save(ms, ImageFormat.Png);
            Response.ClearContent(); //需要输出图象信息 要修改HTTP头
            Response.ContentType = "image/Png";
            Response.BinaryWrite(ms.ToArray());
            theGraphics.Dispose();
            theBitmap.Dispose();
            Response.End();
        }
    }
    #endregion
    #region 发送大文件到客户段
    public class UploadFile
    {
        public UploadFile()
        {
        }
        public void Upload(HttpResponse Response)
        {
            System.IO.Stream iStream = null;
            byte[] buffer=new Byte[10000];// Buffer to read 10K bytes in chunk
            int length;//length of file
            long dataToRead;//total data to read;
            string filepath = "D:"; //Identity the file to download including its path
            string filename = System.IO.Path.GetFileName(filepath);// Identity the file name
            try
            {
                iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

                dataToRead = iStream.Length;//Total bytes to read
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment:filename=" + filename);
                //Read bytes
                while (dataToRead > 0)
                {
                    //verify the client that is connected
                    if (Response.IsClientConnected)
                    {
                        //Read the data in buffer
                        length = iStream.Read(buffer, 0, 1000);
                        //Write the data to the current output stream
                        Response.OutputStream.Write(buffer, 0, length);
                        //flush the data to the HTML output
                        Response.Flush();
                        buffer = new Byte[10000];
                        dataToRead = dataToRead - 10000;

                    }
                    else
                    {
                        //prevent infinite if use discontents
                        dataToRead = -1;
                    }
                }

            }
            catch (Exception ex)
            {
                //Trap the error,if any
                Response.Write("Error:" + ex.Message);
            }
            finally
            {
                if (iStream != null)
                {
                    //Close the file
                    iStream.Close();
                }
            }
           

        }
    }
    #endregion
    public class Citys
    {
        public Citys()
        {
        }

        #region 获取省份列表
        /// <summary>
        /// 查询
        /// </summary>
        ///
        /// <returns></returns>
        public DataSet ProvinceList()
        {
            DataSet ds = null;
            DataBase data = new DataBase();
            try
            {
                ds = data.RunProcSet("sp_province_searching");
            }
            catch (Exception ex)
            {
                Error.Log(ex.Message);
            }
            return ds;
        }
        #endregion
        #region 获取城市列表
        /// <summary>
        /// 查询
        /// </summary>
        ///
        /// <returns></returns>
        public DataSet CityList(string ProvinceID)
        {
            DataSet ds = null;
            DataBase data = new DataBase();
            SqlParameter[] prams ={
                data.MakeInParam("@id",SqlDbType.NVarChar,20,ProvinceID),
             };
            try
            {
                ds = data.RunProcSet("sp_city_searching",prams);
            }
            catch (Exception ex)
            {
                Error.Log(ex.Message);
            }
            return ds;
        }
        #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值