欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

向指定单元格写入数据

程序员文章站 2022-07-13 13:31:12
...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Diagnostics;


namespace WindowsFormsApplication1

{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            new MyExit();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals(""))
            {
                MessageBox.Show("金额不能为空!");
            }
            else
            {
                try
                {
                    FileStream fs = new FileStream(@"C:\d.xls", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    HSSFWorkbook wb = new HSSFWorkbook(fs);
                    fs.Close();
                    HSSFSheet sh = (HSSFSheet)wb.GetSheetAt(1);
                    sh.GetRow(1).GetCell(1).SetCellValue(textBox1.Text);

                    FileStream fsout = new FileStream(@"C:\d.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    wb.Write(fsout);
                    fsout.Close();

                }
                catch
                {
                    MessageBox.Show("运行到这里可能是读写文件异常!");
                }
                Process.Start(@"C:\Program Files\Office2007\excel.exe", "c:\\d.xls");
            }
        }
    }

    class MyExit {
        public MyExit() {
            Application.Exit();
        }
    }

}

 

相关标签: c#