我们“老板”让我记录 http://www.cjh.com.cn/ 上面的长江水文数据!此网站有个变态的事情就是数据只是在一段时间内才有,例如:我是要记录早晨8时的数据,而此数据一般只出现在早晨8时和早晨9时之间,一旦到了9时之后,数据可能就要更新了,最开始的时候,我都是早晨起来用 Weibo 截图发布来记录,知道有一天我突发奇想,我还是写个程序吧,于是此程序就出现了!
首先看看预览吧!
基本思路:
遇到的问题:
具体实现步骤如下:
public struct Info //用来记录表格中的四个属性数据 { public string name; public string date; public string water; public string stream; } //引用:using Excel = Microsoft.Office.Interop.Excel; Excel.Application ex = new Excel.Application(); Excel.Workbook eWorkbook; Excel.Worksheet eWorksheet; int countRecorderDay = 0; //用来记录白天的记载次数 int countRecorderNight = 0; //用来记录夜间的记录次数 int count = 0; //倒计时来用 string saveFilePath = Environment.CurrentDirectory + @"\长江水文\长江水文.xlsx"; //Excel的文件地址
timer1:用来记录总时间,每隔1分钟记录一次,到达 8:30 启动 timer2;
timer2:记录局部操作,开始刷新网页;
timer3:倒计时;
notifyIcon1:用来显示在任务栏中的通知;
contextMenuStrip2:用来显示通知的右键菜单。
timer1_Tick 事件:
private void timer1_Tick(object sender, EventArgs e) //计时器1 { if ((DateTime.Now.Hour == 8 && DateTime.Now.Minute >= 30 && countRecorderDay == 0) || (DateTime.Now.Hour == 20 && DateTime.Now.Minute >= 30 && countRecorderNight == 0)) { timer2.Enabled = true; //开启 timer2 lbTimer2.Text = "计时器2开启!"; lbTimer1.Text = "计时器1关闭!"; timer1.Enabled = false; return; } if (DateTime.Now.Hour == 23 && DateTime.Now.Minute >= 55) //夜间11点,将数据归零,等待第二天记录 { countRecorderDay = 0; countRecorderNight = 0; } lbTimer1.Text = "计时器1开启!"; lbRecord.Text = "上一次操作时间:" + DateTime.Now.ToShortTimeString(); //记录 timer1 操作时间 上次操作时间ToolStripMenuItem.Text = lbRecord.Text; }
Form1_Load 事件:
private void Form1_Load(object sender, EventArgs e) { webBrowser1.Url = new System.Uri("http://www.cjh.com.cn/"); lbRecord.Text = "上一次操作时间:" + DateTime.Now.ToShortTimeString(); 上次操作时间ToolStripMenuItem.Text = lbRecord.Text; }
btRefresh_Click 事件:
private void btRefresh_Click(object sender, EventArgs e) //刷新网站 { webBrowser1.Url = new System.Uri("http://www.cjh.com.cn/"); }
btShowData_Click 事件:
private void btShowData_Click(object sender, EventArgs e) //数据出表 { webBrowser1.Url = new System.Uri("http://www.cjh.com.cn/"); string strText = null; try { strText = webBrowser1.Document.Body.InnerText; } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); } if (strText == null) return; int indexFront = strText.IndexOf("寸滩"); int indexAfter = strText.IndexOf("防汛测报"); string text = strText.Substring(indexFront, indexAfter - indexFront); string[] strLines = new string[20]; strLines = text.Split(new char[] { '\n' }); List<Info> content = new List<Info>(); for (int i = 0; i < strLines.Length;i++ ) { string[] parts = new string[5]; if (strLines[i].Length < 5) break; parts = strLines[i].Split(new char[] { ' ' }); Info inner = new Info(); inner.name = parts[0]; inner.date = parts[1]; inner.water = parts[2].Substring(0, parts[2].IndexOf(".") + 3); inner.stream = parts[2].Substring(parts[2].IndexOf(".") + 3); content.Add(inner); } DataTable table = new DataTable(); DataColumn columnName = new DataColumn("站点"); DataColumn columnDate = new DataColumn("时间"); DataColumn columnWater = new DataColumn("水位"); DataColumn columnStream = new DataColumn("流量"); table.Columns.Add(columnName); table.Columns.Add(columnDate); table.Columns.Add(columnWater); table.Columns.Add(columnStream); foreach (Info info in content) { DataRow row = table.NewRow(); row[0] = info.name; row[1] = info.date; row[2] = info.water; row[3] = info.stream; table.Rows.Add(row); } dataGridView1.DataSource = table; }
btRecorder_Click 事件:
private void btRecorder_Click(object sender, EventArgs e) //记录数据 { #region 获取网站的中的数据,并存到 List 中 webBrowser1.Url = new System.Uri("http://www.cjh.com.cn/"); //刷新网站 string strText = webBrowser1.Document.Body.InnerText; if (strText == null) return; int indexFront = strText.IndexOf("寸滩"); int indexAfter = strText.IndexOf("防汛测报"); string text = null; if (indexFront > 0 && indexAfter > indexFront) text = strText.Substring(indexFront, indexAfter - indexFront); //获取有用的部分 else return; string[] strLines = new string[20]; strLines = text.Split(new char[] { '\n' }); //将内容按回车分割成数组 List<Info> content = new List<Info>(); //获取数据的每一个部分 for (int i = 0; i < strLines.Length; i++) { string[] parts = new string[5]; if (strLines[i].Length < 5) break; parts = strLines[i].Split(new char[] { ' ' }); Info inner = new Info(); inner.name = parts[0]; inner.date = parts[1]; inner.water = parts[2].Substring(0, parts[2].IndexOf(".") + 3); inner.stream = parts[2].Substring(parts[2].IndexOf(".") + 3); content.Add(inner); } #endregion int countDay = 0; int countNight = 0; foreach (Info info in content) { if (info.date.Contains("8时")) //获取8时的数据 countDay++; else if (info.date.Contains("20时")) countNight++; } #region 定义 lunarMonth 和 lunarDay Hashtable lunarMonth = new Hashtable(); lunarMonth.Add(1, "正"); lunarMonth.Add(2, "二"); lunarMonth.Add(3, "三"); lunarMonth.Add(4, "四"); lunarMonth.Add(5, "五"); lunarMonth.Add(6, "六"); lunarMonth.Add(7, "七"); lunarMonth.Add(8, "八"); lunarMonth.Add(9, "九"); lunarMonth.Add(10, "十"); lunarMonth.Add(11, "十一"); lunarMonth.Add(12, "十二"); Hashtable lunarDay = new Hashtable(); lunarDay.Add(1, "初一"); lunarDay.Add(2, "初二"); lunarDay.Add(3, "初三"); lunarDay.Add(4, "初四"); lunarDay.Add(5, "初五"); lunarDay.Add(6, "初六"); lunarDay.Add(7, "初七"); lunarDay.Add(8, "初八"); lunarDay.Add(9, "初九"); lunarDay.Add(10, "初十"); lunarDay.Add(11, "十一"); lunarDay.Add(12, "十二"); lunarDay.Add(13, "十三"); lunarDay.Add(14, "十四"); lunarDay.Add(15, "十五"); lunarDay.Add(16, "十六"); lunarDay.Add(17, "十七"); lunarDay.Add(18, "十八"); lunarDay.Add(19, "十九"); lunarDay.Add(20, "二十"); lunarDay.Add(21, "二十一"); lunarDay.Add(22, "二十二"); lunarDay.Add(23, "二十三"); lunarDay.Add(24, "二十四"); lunarDay.Add(25, "二十五"); lunarDay.Add(26, "二十六"); lunarDay.Add(27, "二十七"); lunarDay.Add(28, "二十八"); lunarDay.Add(29, "二十九"); lunarDay.Add(30, "三十"); #endregion if (countDay == 11) { #region 8时 string excelPath = saveFilePath; eWorkbook = ex.Workbooks.Open(excelPath); //打开文件,赋值到工作簿 ex.Visible = false; //程序显示 eWorksheet = (Excel.Worksheet)eWorkbook.Sheets[1]; //获取第一个工作表 //新格式 string date = DateTime.Today.Month + "月" + DateTime.Today.Day + "日"; ChineseLunisolarCalendar lunar = new ChineseLunisolarCalendar(); string dateLunar = lunarMonth[GetMonthFromYear(DateTime.Today.Date)] + "月" + lunarDay[lunar.GetDayOfMonth(DateTime.Today.Date)]; int row = ex.Application.get_Range("B65535", Type.Missing).get_End(Excel.XlDirection.xlUp).Row; eWorksheet.get_Range("A" + (row + 1).ToString()).Value = date; eWorksheet.get_Range("B" + (row + 1).ToString()).Value = DateTime.Now.Hour + "时"; eWorksheet.get_Range("C" + (row + 1).ToString()).Value = dateLunar; eWorksheet.get_Range("D" + (row + 1).ToString()).Value = content[0].water; eWorksheet.get_Range("E" + (row + 1).ToString()).Value = content[0].stream; eWorksheet.get_Range("F" + (row + 1).ToString()).Value = content[1].water; eWorksheet.get_Range("G" + (row + 1).ToString()).Value = content[1].stream; eWorksheet.get_Range("H" + (row + 1).ToString()).Value = content[2].water; eWorksheet.get_Range("I" + (row + 1).ToString()).Value = content[2].stream; eWorksheet.get_Range("J" + (row + 1).ToString()).Value = content[3].water; eWorksheet.get_Range("K" + (row + 1).ToString()).Value = content[3].stream; eWorksheet.get_Range("L" + (row + 1).ToString()).Value = content[4].water; eWorksheet.get_Range("M" + (row + 1).ToString()).Value = content[4].stream; eWorksheet.get_Range("N" + (row + 1).ToString()).Value = content[5].water; eWorksheet.get_Range("O" + (row + 1).ToString()).Value = content[5].stream; eWorksheet.get_Range("P" + (row + 1).ToString()).Value = content[6].water; eWorksheet.get_Range("Q" + (row + 1).ToString()).Value = content[6].stream; eWorksheet.get_Range("R" + (row + 1).ToString()).Value = content[7].water; eWorksheet.get_Range("S" + (row + 1).ToString()).Value = content[7].stream; eWorksheet.get_Range("T" + (row + 1).ToString()).Value = content[8].water; eWorksheet.get_Range("U" + (row + 1).ToString()).Value = content[8].stream; eWorksheet.get_Range("V" + (row + 1).ToString()).Value = content[9].water; eWorksheet.get_Range("W" + (row + 1).ToString()).Value = content[9].stream; eWorksheet.get_Range("X" + (row + 1).ToString()).Value = content[10].water; eWorksheet.get_Range("Y" + (row + 1).ToString()).Value = content[10].stream; eWorkbook.Save(); ex.Quit(); this.Focus(); timer2.Enabled = false; lbTimer2.Text = "计时器2关闭!"; timer1.Enabled = true; lbTimer1.Text = "计时器1开启!"; label4.Text = DateTime.Now.ToShortDateString() + " 8时 记录完毕!"; countRecorderDay++; #endregion } if (countNight == 11) { #region 20时 string excelPath = saveFilePath; eWorkbook = ex.Workbooks.Open(excelPath); //打开文件,赋值到工作簿 ex.Visible = false; //程序显示 eWorksheet = (Excel.Worksheet)eWorkbook.Sheets[1]; //获取第一个工作表 //新格式 string date = DateTime.Today.Month + "月" + DateTime.Today.Day + "日"; ChineseLunisolarCalendar lunar = new ChineseLunisolarCalendar(); string dateLunar = lunarMonth[GetMonthFromYear(DateTime.Today.Date)] + "月" + lunarDay[lunar.GetDayOfMonth(DateTime.Today.Date)]; int row = ex.Application.get_Range("B65535", Type.Missing).get_End(Excel.XlDirection.xlUp).Row; //eWorksheet.get_Range("A" + (row + 1).ToString()).Value = date; eWorksheet.get_Range("B" + (row + 1).ToString()).Value = DateTime.Now.Hour + "时"; //eWorksheet.get_Range("C" + (row + 1).ToString()).Value = dateLunar; eWorksheet.get_Range("D" + (row + 1).ToString()).Value = content[0].water; eWorksheet.get_Range("E" + (row + 1).ToString()).Value = content[0].stream; eWorksheet.get_Range("F" + (row + 1).ToString()).Value = content[1].water; eWorksheet.get_Range("G" + (row + 1).ToString()).Value = content[1].stream; eWorksheet.get_Range("H" + (row + 1).ToString()).Value = content[2].water; eWorksheet.get_Range("I" + (row + 1).ToString()).Value = content[2].stream; eWorksheet.get_Range("J" + (row + 1).ToString()).Value = content[3].water; eWorksheet.get_Range("K" + (row + 1).ToString()).Value = content[3].stream; eWorksheet.get_Range("L" + (row + 1).ToString()).Value = content[4].water; eWorksheet.get_Range("M" + (row + 1).ToString()).Value = content[4].stream; eWorksheet.get_Range("N" + (row + 1).ToString()).Value = content[5].water; eWorksheet.get_Range("O" + (row + 1).ToString()).Value = content[5].stream; eWorksheet.get_Range("P" + (row + 1).ToString()).Value = content[6].water; eWorksheet.get_Range("Q" + (row + 1).ToString()).Value = content[6].stream; eWorksheet.get_Range("R" + (row + 1).ToString()).Value = content[7].water; eWorksheet.get_Range("S" + (row + 1).ToString()).Value = content[7].stream; eWorksheet.get_Range("T" + (row + 1).ToString()).Value = content[8].water; eWorksheet.get_Range("U" + (row + 1).ToString()).Value = content[8].stream; eWorksheet.get_Range("V" + (row + 1).ToString()).Value = content[9].water; eWorksheet.get_Range("W" + (row + 1).ToString()).Value = content[9].stream; eWorksheet.get_Range("X" + (row + 1).ToString()).Value = content[10].water; eWorksheet.get_Range("Y" + (row + 1).ToString()).Value = content[10].stream; eWorkbook.Save(); ex.Quit(); this.Focus(); //Application.Exit(); timer2.Enabled = false; lbTimer2.Text = "计时器2关闭!"; timer1.Enabled = true; lbTimer1.Text = "计时器1开启!"; label4.Text = DateTime.Now.ToShortDateString() + " 20时 记录完毕!"; countRecorderNight++; #endregion } }
btOpenFile_Click 事件:
private void btOpenFile_Click(object sender, EventArgs e) //打开工作簿 { if (btOpenFile.Text == "打开Excel查看") { string excelPath = saveFilePath; eWorkbook = ex.Workbooks.Open(excelPath); //打开文件,赋值到工作簿 int rowNum = ex.Application.get_Range("B65535", Type.Missing).get_End(Excel.XlDirection.xlUp).Row; eWorksheet = (Excel.Worksheet)eWorkbook.Sheets[1]; eWorksheet.get_Range("B" + (rowNum + 5).ToString()).Value = null; //保证滚到此位置 ex.Visible = true; btOpenFile.Text = "保存关闭Excel"; } else if(btOpenFile.Text == "保存关闭Excel") { eWorkbook.Save(); ex.Quit(); btOpenFile.Text = "打开Excel查看"; } }
btDownloadData_Click 事件:
private void btDownloadData_Click(object sender, EventArgs e) //保存当前数据 { #region 获取网站的中的数据,并存到 List 中 webBrowser1.Url = new System.Uri("http://www.cjh.com.cn/"); //刷新网站 string strText = webBrowser1.Document.Body.InnerText; if (strText == null) return; int indexFront = strText.IndexOf("寸滩"); int indexAfter = strText.IndexOf("防汛测报"); string text = null; if (indexFront > 0 && indexAfter > indexFront) { text = strText.Substring(indexFront, indexAfter - indexFront); //获取有用的部分 } else { return; } string[] strLines = new string[20]; strLines = text.Split(new char[] { '\n' }); //将内容按回车分割成数组 List<Info> content = new List<Info>(); //获取数据的每一个部分 for (int i = 0; i < strLines.Length; i++) { string[] parts = new string[5]; if (strLines[i].Length < 5) break; parts = strLines[i].Split(new char[] { ' ' }); Info inner = new Info(); inner.name = parts[0]; inner.date = parts[1]; inner.water = parts[2].Substring(0, parts[2].IndexOf(".") + 3); inner.stream = parts[2].Substring(parts[2].IndexOf(".") + 3); content.Add(inner); } #endregion int countDay = 0; int countNight = 0; foreach (Info info in content) { if (info.date.Contains("8时")) //获取8时的数据 countDay++; else if (info.date.Contains("20时")) countNight++; } #region 定义 lunarMonth 和 lunarDay Hashtable lunarMonth = new Hashtable(); lunarMonth.Add(1, "正"); lunarMonth.Add(2, "二"); lunarMonth.Add(3, "三"); lunarMonth.Add(4, "四"); lunarMonth.Add(5, "五"); lunarMonth.Add(6, "六"); lunarMonth.Add(7, "七"); lunarMonth.Add(8, "八"); lunarMonth.Add(9, "九"); lunarMonth.Add(10, "十"); lunarMonth.Add(11, "十一"); lunarMonth.Add(12, "十二"); Hashtable lunarDay = new Hashtable(); lunarDay.Add(1, "初一"); lunarDay.Add(2, "初二"); lunarDay.Add(3, "初三"); lunarDay.Add(4, "初四"); lunarDay.Add(5, "初五"); lunarDay.Add(6, "初六"); lunarDay.Add(7, "初七"); lunarDay.Add(8, "初八"); lunarDay.Add(9, "初九"); lunarDay.Add(10, "初十"); lunarDay.Add(11, "十一"); lunarDay.Add(12, "十二"); lunarDay.Add(13, "十三"); lunarDay.Add(14, "十四"); lunarDay.Add(15, "十五"); lunarDay.Add(16, "十六"); lunarDay.Add(17, "十七"); lunarDay.Add(18, "十八"); lunarDay.Add(19, "十九"); lunarDay.Add(20, "二十"); lunarDay.Add(21, "二十一"); lunarDay.Add(22, "二十二"); lunarDay.Add(23, "二十三"); lunarDay.Add(24, "二十四"); lunarDay.Add(25, "二十五"); lunarDay.Add(26, "二十六"); lunarDay.Add(27, "二十七"); lunarDay.Add(28, "二十八"); lunarDay.Add(29, "二十九"); lunarDay.Add(30, "三十"); #endregion if (true) { #region 当前时刻 string excelPath = saveFilePath; eWorkbook = ex.Workbooks.Open(excelPath); //打开文件,赋值到工作簿 ex.Visible = false; //程序显示 eWorksheet = (Excel.Worksheet)eWorkbook.Sheets[1]; //获取第一个工作表 //新格式 string date = DateTime.Today.Month + "月" + DateTime.Today.Day + "日"; ChineseLunisolarCalendar lunar = new ChineseLunisolarCalendar(); string dateLunar = lunarMonth[GetMonthFromYear(DateTime.Today.Date)] + "月" + lunarDay[lunar.GetDayOfMonth(DateTime.Today.Date)]; int row = ex.Application.get_Range("B65535", Type.Missing).get_End(Excel.XlDirection.xlUp).Row; eWorksheet.get_Range("A" + (row + 1).ToString()).Value = date; eWorksheet.get_Range("B" + (row + 1).ToString()).Value = DateTime.Now.ToShortTimeString(); eWorksheet.get_Range("C" + (row + 1).ToString()).Value = dateLunar; eWorksheet.get_Range("D" + (row + 1).ToString()).Value = content[0].water; eWorksheet.get_Range("E" + (row + 1).ToString()).Value = content[0].stream; eWorksheet.get_Range("F" + (row + 1).ToString()).Value = content[1].water; eWorksheet.get_Range("G" + (row + 1).ToString()).Value = content[1].stream; eWorksheet.get_Range("H" + (row + 1).ToString()).Value = content[2].water; eWorksheet.get_Range("I" + (row + 1).ToString()).Value = content[2].stream; eWorksheet.get_Range("J" + (row + 1).ToString()).Value = content[3].water; eWorksheet.get_Range("K" + (row + 1).ToString()).Value = content[3].stream; eWorksheet.get_Range("L" + (row + 1).ToString()).Value = content[4].water; eWorksheet.get_Range("M" + (row + 1).ToString()).Value = content[4].stream; eWorksheet.get_Range("N" + (row + 1).ToString()).Value = content[5].water; eWorksheet.get_Range("O" + (row + 1).ToString()).Value = content[5].stream; eWorksheet.get_Range("P" + (row + 1).ToString()).Value = content[6].water; eWorksheet.get_Range("Q" + (row + 1).ToString()).Value = content[6].stream; eWorksheet.get_Range("R" + (row + 1).ToString()).Value = content[7].water; eWorksheet.get_Range("S" + (row + 1).ToString()).Value = content[7].stream; eWorksheet.get_Range("T" + (row + 1).ToString()).Value = content[8].water; eWorksheet.get_Range("U" + (row + 1).ToString()).Value = content[8].stream; eWorksheet.get_Range("V" + (row + 1).ToString()).Value = content[9].water; eWorksheet.get_Range("W" + (row + 1).ToString()).Value = content[9].stream; eWorksheet.get_Range("X" + (row + 1).ToString()).Value = content[10].water; eWorksheet.get_Range("Y" + (row + 1).ToString()).Value = content[10].stream; eWorkbook.Save(); ex.Quit(); this.Focus(); label4.Text = DateTime.Now.ToShortDateString() + DateTime.Now.ToShortTimeString() + "记录完毕!"; #endregion } }
其他杂项 - 包括函数以及菜单点击事件和通知相关操作
private int GetMonthFromYear(DateTime time) //获取农历数据中的月份 { ChineseLunisolarCalendar clc = new ChineseLunisolarCalendar(); if (clc.GetLeapMonth(time.Year) != 0) //存在闰月 { if (clc.GetMonth(time) > clc.GetLeapMonth(time.Year)) //如果月份大于闰月 { return clc.GetMonth(time) - 1; } } return clc.GetMonth(time); } private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Visible = false; this.notifyIcon1.Visible = true; } } private void notifyIcon1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (this.Visible == false) { this.Visible = true; this.WindowState = FormWindowState.Normal; } else { this.Visible = false; this.WindowState = FormWindowState.Minimized; } } else if (e.Button == MouseButtons.Right) { notifyIcon1.ContextMenuStrip = contextMenuStrip2; } } private void 显示主窗体ToolStripMenuItem_Click(object sender, EventArgs e) { this.Visible = true; this.Focus(); this.WindowState = FormWindowState.Normal; } private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e) { Application.Exit(); } private void 主窗体显示ToolStripMenuItem_Click(object sender, EventArgs e) { this.Visible = true; this.Focus(); this.WindowState = FormWindowState.Normal; } private void 打开Excel查看ToolStripMenuItem_Click(object sender, EventArgs e) { btOpenFile_Click(btOpenFile, e); 打开Excel查看ToolStripMenuItem.Text = btOpenFile.Text; } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); }
timer2_Tick 事件:
private void timer2_Tick(object sender, EventArgs e) //计时器2 { label4.Text = "上一次操作时间:" + DateTime.Now.ToShortTimeString(); btRecorder_Click(btRecorder, e); }
timer3_Tick 事件:
private void timer3_Tick(object sender, EventArgs e) //计时器3 { lbCountdown.Text = String.Format("{0:000}",(60 - (count % 60))); count++; if (DateTime.Now.Hour == 8 && DateTime.Now.Minute == 59 && DateTime.Now.Second == 30) //最后时刻监测,若是还没有添加数据,则主动添加,并打开网页! { if (countRecorderDay == 0) { btDownloadData_Click(btDownloadData, e); System.Diagnostics.Process.Start("http://www.cjh.com.cn/"); timer1.Enabled = true; timer2.Enabled = false; } } if (DateTime.Now.Hour == 20 && DateTime.Now.Minute == 59 && DateTime.Now.Second == 30) { if (countRecorderNight == 0) { btDownloadData_Click(btDownloadData, e); System.Diagnostics.Process.Start("http://www.cjh.com.cn/"); timer1.Enabled = true; timer2.Enabled = false; } } if (DateTime.Now.Hour == 10 && DateTime.Now.Minute == 30 && DateTime.Now.Second == 30) //承接上面,若数据添加不全,则在一个半小时后再次添加! { btRefresh_Click(btRefresh, e); //否则在执行20时的时候,此时网页还是8时的效果,因此还会加载一次! if (countRecorderDay == 0) { btDownloadData_Click(btDownloadData, e); System.Diagnostics.Process.Start("http://www.cjh.com.cn/"); } } if (DateTime.Now.Hour == 22 && DateTime.Now.Minute == 30 && DateTime.Now.Second == 30) { btRefresh_Click(btRefresh, e); if (countRecorderNight == 0) { btDownloadData_Click(btDownloadData, e); System.Diagnostics.Process.Start("http://www.cjh.com.cn/"); } } }
修改:
1. 在加入文本的时候,之前是按字符串添加,因此到达Excel中也是字符串格式的数字文本,修改如下,将字符串转为数字格式再加入到Excel中!
eWorksheet.get_Range("D" + (row + 1).ToString()).Value = Convert.ToSingle(content[0].water); eWorksheet.get_Range("E" + (row + 1).ToString()).Value = Convert.ToSingle(content[0].stream); eWorksheet.get_Range("F" + (row + 1).ToString()).Value = Convert.ToSingle(content[1].water); eWorksheet.get_Range("G" + (row + 1).ToString()).Value = Convert.ToSingle(content[1].stream);
2. 若是直接将Excel中的文本转为数字类型,要首先将单元格的Value值转为String,如下所示!
for (int i = 4; i <= 56;i++ ) { eWorksheet.get_Range(str + i.ToString()).Value = Convert.ToSingle(eWorksheet.get_Range(str + i.ToString()).Value.ToString()); }