npoi io实际应用

 private void button1_Click(object sender, EventArgs e)
        {
            string[] lines = File.ReadLines(@"D:\2.txt", Encoding.Default).ToArray();
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.CreateSheet();
            //HSSFRow row = sheet.CreateRow(0);
            //HSSFCell cell = row.CreateCell(0, HSSFCell.CELL_TYPE_STRING);
            //cell.SetCellValue()
            for (int i = 0; i < lines.Count(); i++)
            {
                string line = lines[i];
                string[] strs = line.Split('');
                HSSFRow row = sheet.CreateRow(i);
                if (strs.Length <= 1)
                {
                    HSSFCell cell2 = row.CreateCell(1, HSSFCell.CELL_TYPE_STRING);
                  // MessageBox.Show(strs[0]);
                    cell2.SetCellValue(strs[0]);
                }
                if (strs.Length >= 2)
                {
                    string fu = strs[0];
                    string jue = strs[1];
                    //MessageBox.Show(fu + jue);
                    HSSFCell cell0 = row.CreateCell(0, HSSFCell.CELL_TYPE_STRING);
                    HSSFCell cell1 = row.CreateCell(1, HSSFCell.CELL_TYPE_STRING);
                    cell0.SetCellValue(fu);
                    cell1.SetCellValue(jue);
                }
                
            }
            using (FileStream stream = new FileStream(@"d:\统计.xls", FileMode.OpenOrCreate, FileAccess.Write))
            {
                workbook.Write(stream);
            }
            MessageBox.Show("导入完成!!大英哥真厉害");
        }

        private void btTiqu_Click(object sender, EventArgs e)
        {
            using (StreamReader sr = new StreamReader(@"D:\1.txt"))
            {
                string s = sr.ReadToEnd();
                //开始字符串 
                string s1 = textBox1.Text;
                //结束字符串 
                string s2 = textBox2.Text;
                //Regex rg = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + s2 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
                Regex rg = new Regex("(?<=(" + s1 + "))[^(" + s1 + ")]*?(?=(" + s2 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
                foreach (var m in rg.Matches(s))
                {
                    StreamWriter sw = new StreamWriter(@"D:\2.txt", true);
                    sw.WriteLine(m.ToString());
                    sw.Close();

                }
            }
            MessageBox.Show("well done !");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.AcceptButton =btTiqu;
        }
    }

 

posted @ 2013-11-13 18:55  南瓜asp  阅读(147)  评论(0)    收藏  举报