public Dist(string city, string distn,string villa, string year, cont[] cont) { this.city = city; this.distn = distn; this.villa = villa; this.year = year; this.cont = cont; }
static void Main(string[] args) { string docname = "filename_Zhongzheng.txt"; StreamReader input = new StreamReader(docname, System.Text.Encoding.Default); //指定字元編碼方式(中文) int leng = 30; string[] list = new string[leng]; for (int i = 0; i < leng; i++) { string line = input.ReadLine(); list[i] = line; } int len = list.Length; Dist[] Daan = new Dist[len]; //int k = 3; for (int k = 0; k < len; k++) { string strfile = "D:\\CP\\" + list[k] + ".xls", outfile = "D:\\CP\\" + list[k] + ".json"; string nam = list[k].Replace("經費收支結算表", ""); string distname = nam.Substring(0, 3); string villname = nam.Substring(3, 3); string strFilePath = string.Format(@strfile);// D:\CP HSSFWorkbook wk; using (FileStream fs = new FileStream(strFilePath, FileMode.Open)) { wk = new HSSFWorkbook(fs); } //取得sheet名稱 HSSFSheet hst; hst = (HSSFSheet)wk.GetSheetAt(0); string strSheetname = hst.SheetName; //單行單行的取得sheet理的資料 HSSFRow hr; hr = (HSSFRow)hst.GetRow(0); string head = hr.Cells[0].ToString(); head = head.Trim(' '); head = head.Replace(" ", "").Replace(" ", ""); Console.WriteLine(head); string year = head.Substring(head.IndexOf("1"), 3); hr = (HSSFRow)hst.GetRow(2); int n = hr.LastCellNum; string[] header = new string[15]; for (int i = 1; i < n - 1; i++) { string b = hr.Cells[i].ToString(); int test = b.IndexOf("\n"); if (test > 0) { b = b.Remove(test, 1); } header[i] = b; } hr = (HSSFRow)hst.GetRow(3); string[] plan = new string[15]; for (int i = 1; i < n - 1; i++) { string b; if (hr.Cells[i].ToString().Length > 1) { b = hr.Cells[i].ToString().Replace("*", "").Replace(" ", "").Replace("$", "").Replace("元", "").Replace("NT", ""); } else { b = "0"; } int value2 = (int)decimal.Parse(b);// 96,765.00 --> 96765 b = Convert.ToString(value2); plan[i] = b; } hr = (HSSFRow)hst.GetRow(4); string[] res = new string[15]; for (int i = 1; i < n - 1; i++) { string b; if (hr.Cells[i].ToString().Length > 1) { b = hr.Cells[i].ToString().Replace("*", "").Replace(" ", "").Replace("$", "").Replace("元", "").Replace("NT", ""); } else { b = "0"; } int value2 = (int)decimal.Parse(b);// 96,765.00 --> 96765 b = Convert.ToString(value2); //b = string.Format("{0:C}", value2); // 96765 --> NT$96,765.00 //b = string.Format("{0:NT$#,0}", value2); // 96765 --> NT$96,765 res[i] = b; } cont[] cont = new cont[n - 2]; string jsonoutstr = ""; for (int i = 1; i < n - 1; i++) { cont[i - 1] = new cont(header[i], plan[i], res[i]); string jsonout = JsonConvert.SerializeObject(cont[i - 1]); jsonoutstr += jsonout + "\n"; } Console.WriteLine(jsonoutstr); Daan[k] = new Dist("台北市", distname, villname, year, cont); //Console.WriteLine(JsonConvert.SerializeObject(Daan[k]) + "\n"); // System.IO.File.WriteAllText(@outfile, jsonoutstr); //Console.ReadLine(); } string jo = JsonConvert.SerializeObject(Daan); // Console.WriteLine(jo); string refile = "D:\\CP\\" + "Zhongzhengdistfonddata" + ".json"; System.IO.File.WriteAllText(@refile, jo); //Console.ReadLine(); }