当前位置:首页 > Windows程序 > 正文

账单1.4 :NPOI +Excel Winform版

2024-03-31 Windows程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPOI;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.SS.Formula.Functions;
using ICSharpCode.SharpZipLib;

namespace 账单NPOI_Excel_WinForm
{
    public partial class Form1 : Form
    {
        int lastrow;
        int lastcolumn;
        string path = @"J:\a.xls";
        HSSFWorkbook workbook;

        public Form1()
        {
            InitializeComponent();

            //初始化
            using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

            {
                workbook = new HSSFWorkbook(fs);//初始化时,,即建立workbook.
                fs.Close();
            }
            ISheet sheet2 = workbook.GetSheet("list");//在listBOX中填加内容
            for (int i = 0; i < sheet2.LastRowNum + 1; i++)
            {
                IRow row = sheet2.GetRow(i);
                ICell cell = row.GetCell(0);
                listBox1.Items.Add(cell.ToString());
                listBox2.Items.Add(cell.ToString());

            }
            //以下为读取当前的金额数值

            ISheet sheet = workbook.GetSheet("main");
            IRow moneyrow = sheet.GetRow(1);
            IRow titlerow = sheet.GetRow(0);

            for (int i = 1; i <= 10; i++)
            {
                tx1 = (TextBox)this.Controls.Find("textBox" + i.ToString(), true)[0];
                //tx2 = (TextBox)this.Controls.Find("textBox" + (i+10).ToString(), true)[0];
                lab1 = (Label)this.Controls.Find("label" + i.ToString(), true)[0];
              
                ICell cell = moneyrow.GetCell(i);
                tx1.Text = cell.ToString();
                ICell cell1 = titlerow.GetCell(i);
                lab1.Text = cell1.ToString();
            }

            double total=0;
            for (int i = 1; i < 10; i++)
            {
                tx1 = (TextBox)this.Controls.Find("textBox" + i.ToString(), true)[0];
                total = total + double.Parse(tx1.Text);

            }

            label11.Text = "总金额:" + total.ToString();
        }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/43562.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494595访问次数
  • 建站天数
  • 友情链接