Esempio n. 1
0
        //开始爬取按钮
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //删除data目录下所有文件
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "data/"))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "data/");
            }
            else
            {
                try
                {
                    DirectoryInfo    dir      = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "data/");
                    FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                    foreach (FileSystemInfo i in fileinfo)
                    {
                        if (i is DirectoryInfo)            //判断是否文件夹
                        {
                            DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                            subdir.Delete(true);          //删除子目录和文件
                        }
                        else
                        {
                            File.Delete(i.FullName);      //删除指定文件
                        }
                    }
                }
                catch (Exception a)
                {
                    MessageBox.Show(a.ToString());
                }
            }
            bool isSeven = false;

            if (isSevenDay.IsChecked == true)
            {
                isSeven   = true;
                StartTime = new DateTime(int.Parse(StartPointYears.Text), int.Parse(StartPointMonth.Text), int.Parse(StartPointDay.Text));
                EndTime   = new DateTime(int.Parse(EndPointYears.Text), int.Parse(EndPointMonth.Text), int.Parse(EndPointDay.Text) + 1);
                if (StartTime > EndTime)
                {
                    MessageBox.Show("爬取失败,请确认时间设定是否正确");
                    return;
                }
            }

            //xlsx or xls
            if (UserID.Text.Substring(UserID.Text.Length - 5, 5) == ".xlsx" | UserID.Text.Substring(UserID.Text.Length - 4, 4) == ".xls")
            {
                GetAlldataByExcelFileHandler handler = new GetAlldataByExcelFileHandler(GetAlldataByExcelFile);
                IAsyncResult result = handler.BeginInvoke(UserID.Text, isSeven, new AsyncCallback(FileCallback), null);
            }
            //单个UserID
            else
            {
                GetAllDataHandler handler = new GetAllDataHandler(GetAllData);
                IAsyncResult      result  = handler.BeginInvoke(UserID.Text.Split('_')[1], UserID.Text.Split('_')[0], isSeven, new AsyncCallback(SingleIDCallback), null);
            }
            UnEnabledButton();
            StartButton.Content = "爬取中……";
        }
Esempio n. 2
0
 //爬取所有用户的数据
 private void GetAlldataByExcelFile(string path, bool seven)
 {
     try
     {
         IWorkbook  workbook   = null;                //新建IWorkbook对象
         FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
         if (path.IndexOf(".xlsx") > 0)               // 2007版本
         {
             workbook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook
         }
         else if (path.IndexOf(".xls") > 0)           // 2003版本
         {
             workbook = new HSSFWorkbook(fileStream); //xls数据读入workbook
         }
         ISheet sheet = workbook.GetSheetAt(0);       //获取第一个工作表
         IRow   row;                                  // = sheet.GetRow(0);            //新建当前工作表行数据
         for (int i = 3; i < sheet.LastRowNum; i++)   //对工作表每一行
         {
             row = sheet.GetRow(i);                   //row读入第i行数据
             if (row != null)
             {
                 string            cellValue = row.GetCell(2).ToString();
                 GetAllDataHandler handler   = new GetAllDataHandler(GetAllData);
                 if (!string.IsNullOrWhiteSpace(cellValue))
                 {
                     GetAllData(cellValue.Split('_')[1], cellValue.Split('_')[0], seven);
                 }
             }
         }
         fileStream.Close();
         workbook.Close();
     }
     catch (Exception e)
     {
         MessageBox.Show(e.ToString());
     }
 }