从一个地址中下载Excel表格,然后读取这个Excel表格数据
注:表格是.csv格式的,我试了一下其他格式的这个程序不支持,晚点续更
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Data;
using System.Data.OleDb;
using System.Text.RegularExpressions;
namespace DownLoadExcel
{
public class DownLoadRead
{
private static string directory = System.Configuration.ConfigurationManager.AppSettings["Download_Dic"];//存放地址
private static string url = System.Configuration.ConfigurationManager.AppSettings["url"];//下载地址
/// <summary>
/// 从指定url下载csv数据,然后给文件根据下载时间命名,返回文件名
/// </summary>
/// <param name="url">下载地址</param>
/// <returns>文件名称</returns>
public string DownloadFile()
{
try
{
string fileName = CreateFileName(url);
if (!Directory.Exists(directory))//判断指定路径下是否存在指定的文件夹或文件
{
Directory.CreateDirectory(directory);//创建路径下的文件夹
}
WebClient client = new WebClient();
client.DownloadFile(url, directory + fileName);
string datajson=CsvRead(0, directory + fileName);//读取excel表中数据
//另一种方法,将文件地址封装
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("DownExcellocation", directory + fileName);
//csvRead(0, dic["DownExcellocation"]);
return fileName;
}
catch
{
return "";
}
}
/// <summary>
/// 创建文件名称
/// </summary>
public static string CreateFileName(string url)
{
stri