x01.ExcelHelper: NPOI 操作

来源:http://www.cnblogs.com/china_x01/archive/2017/07/06/7127062.html
-Advertisement-
Play Games

Excel 操作,具有十分明顯的針對性,故很難通用,但這並不妨礙參考後以解決自己的實際問題。 有一彙總表如下: 當然,只是示範,產品的代碼應該唯一!現在要根據此彙總表產生各個客戶的產品清單。由於客戶較多,彙總分表1,表2;客戶清單模板根據產品類別,每個清單又分為三個表。做個模板,然後手工複製粘貼,完 ...


Excel 操作,具有十分明顯的針對性,故很難通用,但這並不妨礙參考後以解決自己的實際問題。

有一彙總表如下:

    

當然,只是示範,產品的代碼應該唯一!現在要根據此彙總表產生各個客戶的產品清單。由於客戶較多,彙總分表1,表2;客戶清單模板根據產品類別,每個清單又分為三個表。做個模板,然後手工複製粘貼,完成需要小半天的時間。還是寫個程式來幫幫忙吧。

首先,是下載 NPOI 庫及參考手冊,花個10來分鐘粗略看看,然後開工。主要代碼如下:

<Window x:Class="x01.ExcelHelper.SplitWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    WindowStartupLocation="CenterScreen"
    Title="x01.SplitWindow" Height="310" Width="480">
        <Window.Resources>
        <Style TargetType="TextBox" x:Key="ShortBoxKey">
            <Setter Property="MinWidth" Value="20" />
            <Setter Property="Margin" Value="5" />
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="Height" Value="20" />
        </Style>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="5" />
            <Setter Property="Height" Value="20" />
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="0 5 0 0" />
            <Setter Property="Height" Value="20" />
        </Style>
    </Window.Resources>
    <Grid Margin="5 10 5 5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right">原始文件:</TextBlock>
        <TextBox Grid.Row="0" Grid.Column="1" Name="tbxOriginPath" /> 
        <Button Grid.Row="0" Grid.Column="2" Width="30" Name="OpenOriginButton"
            Click="OpenOriginButton_Click"></Button>
        <StackPanel Grid.Row="1" Grid.ColumnSpan="3">
            <StackPanel Orientation="Horizontal">
                <TextBlock>原始表1: 表名</TextBlock>
                <TextBox Name="tbxOriginSheet1Name" MinWidth="40" Margin="5 0" />
                <TextBlock>起始行</TextBlock>
                <TextBox Name="tbxOriginSheet1StartRow" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束行</TextBlock>
                <TextBox Name="tbxOriginSheet1EndRow" Style="{StaticResource ShortBoxKey}" />    
                <TextBlock>起始列</TextBlock>
                <TextBox Name="tbxOriginSheet1StartCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束列</TextBlock>
                <TextBox Name="tbxOriginSheet1EndCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>代碼列</TextBlock>
                <TextBox Name="tbxOriginSheet1CodeCol" Style="{StaticResource ShortBoxKey}" />
            </StackPanel>
        <StackPanel Orientation="Horizontal">
                <TextBlock>原始表2: 表名</TextBlock>
                <TextBox Name="tbxOriginSheet2Name" MinWidth="40" Margin="5 0" />
                <TextBlock>起始行</TextBlock>
                <TextBox Name="tbxOriginSheet2StartRow" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束行</TextBlock>
                <TextBox Name="tbxOriginSheet2EndRow" Style="{StaticResource ShortBoxKey}" />    
                <TextBlock>起始列</TextBlock>
                <TextBox Name="tbxOriginSheet2StartCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束列</TextBlock>
                <TextBox Name="tbxOriginSheet2EndCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>代碼列</TextBlock>
                <TextBox Name="tbxOriginSheet2CodeCol" Style="{StaticResource ShortBoxKey}" />
            </StackPanel>
        </StackPanel>
        <TextBlock Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right">模板文件:</TextBlock>
        <TextBox Grid.Row="2" Grid.Column="1" Name="tbxTemplatePath" />
        <Button Grid.Row="2" Grid.Column="2" Width="30" Name="OpenTemplateButton" 
                Click="OpenTemplateButton_Click"></Button>
        <StackPanel Grid.Row="3" Grid.ColumnSpan="3">
            <StackPanel Orientation="Horizontal">
                <TextBlock>模板表1: 表名</TextBlock>
                <TextBox Name="tbxTemplateSheet1Name" MinWidth="40" Margin="5 0" />
                <TextBlock>起始行</TextBlock>
                <TextBox Name="tbxTemplateSheet1StartRow" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束行</TextBlock>
                <TextBox Name="tbxTemplateSheet1EndRow" Style="{StaticResource ShortBoxKey}" />    
                <TextBlock>起始列</TextBlock>
                <TextBox Name="tbxTemplateSheet1StartCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束列</TextBlock>
                <TextBox Name="tbxTemplateSheet1EndCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>代碼列</TextBlock>
                <TextBox Name="tbxTemplateSheet1CodeCol" Style="{StaticResource ShortBoxKey}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock>模板表2: 表名</TextBlock>
                <TextBox Name="tbxTemplateSheet2Name" MinWidth="40" Margin="5 0" />
                <TextBlock>起始行</TextBlock>
                <TextBox Name="tbxTemplateSheet2StartRow" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束行</TextBlock>
                <TextBox Name="tbxTemplateSheet2EndRow" Style="{StaticResource ShortBoxKey}" />    
                <TextBlock>起始列</TextBlock>
                <TextBox Name="tbxTemplateSheet2StartCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束列</TextBlock>
                <TextBox Name="tbxTemplateSheet2EndCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>代碼列</TextBlock>
                <TextBox Name="tbxTemplateSheet2CodeCol" Style="{StaticResource ShortBoxKey}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock>模板表3: 表名</TextBlock>
                <TextBox Name="tbxTemplateSheet3Name" MinWidth="40" Margin="5 0" />
                <TextBlock>起始行</TextBlock>
                <TextBox Name="tbxTemplateSheet3StartRow" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束行</TextBlock>
                <TextBox Name="tbxTemplateSheet3EndRow" Style="{StaticResource ShortBoxKey}" />    
                <TextBlock>起始列</TextBlock>
                <TextBox Name="tbxTemplateSheet3StartCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>結束列</TextBlock>
                <TextBox Name="tbxTemplateSheet3EndCol" Style="{StaticResource ShortBoxKey}" />
                <TextBlock>代碼列</TextBlock>
                <TextBox Name="tbxTemplateSheet3CodeCol" Style="{StaticResource ShortBoxKey}" />
            </StackPanel>
        </StackPanel>
        <Button Grid.Row="4" Grid.ColumnSpan="3" HorizontalAlignment="Right" 
                Name="GenerateFilesButton" Margin="0 10 5 0" Height="32"
                Click="GenerateFilesButton_Click">_Generate Files</Button>
    </Grid>
</Window>
SplitWindow.xaml
/**
 * SplitWindow.cs (c) 2017 by x01
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

using Microsoft.Win32;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace x01.ExcelHelper
{
    /// <summary>
    /// Interaction logic for SplitWindow.xaml
    /// </summary>
    public partial class SplitWindow : Window
    {
        #region Settings
        
        public string OriginPath
        {
            get {
                if (string.IsNullOrEmpty(tbxOriginPath.Text))
                    throw new Exception("請選擇原始文件!");
                return tbxOriginPath.Text;
            }
        }
        
        public string OriginSheet1Name
        {
            get {
                return tbxOriginSheet1Name.Text;
            }
        }
        public int OriginSheet1StartRow
        {
            get {
                int row;
                if (!int.TryParse(tbxOriginSheet1StartRow.Text, out row))
                    throw new Exception("請在表1起始行中填充正確的數字!");
                return row;
            }
        }
        public int OriginSheet1EndRow
        {
            get {
                int row;
                if (!int.TryParse(tbxOriginSheet1EndRow.Text, out row))
                    throw new Exception("請在表1結束行中填入正確的數字!");
                return row;
            }
        }
        public int OriginSheet1StartCol
        {
            get {
                int col;
                if (!int.TryParse(tbxOriginSheet1StartCol.Text, out col))
                    throw new Exception("請在表1起始列中填入正確的數字!");
                return col;
            }
        }
        public int OriginSheet1EndCol
        {
            get {
                int col;
                if (!int.TryParse(tbxOriginSheet1EndCol.Text, out col))
                    throw new Exception("請在表1結束列中填入正確的數字!");
                return col;
            }
        }
        public int OriginSheet1CodeCol
        {
            get {
                int col;
                if (!int.TryParse(tbxOriginSheet1CodeCol.Text, out col))
                    throw new Exception("請在表1代碼列中填入正確的數字!");
                return col;
            }
        }
        
        public string OriginSheet2Name
        {
            get {
                return tbxOriginSheet2Name.Text;
            }
        }
        public int OriginSheet2StartRow
        {
            get {
                int row;
                if (!int.TryParse(tbxOriginSheet2StartRow.Text, out row))
                    throw new Exception("請在表2起始行中填入正確的數字!");
                return row;
            }
        }
        public int OriginSheet2EndRow
        {
            get {
                int row;
                if (!int.TryParse(tbxOriginSheet2EndRow.Text, out row))
                    throw new Exception("請在表2結束行中填入正確的數字!");
                return row;
            }
        }
        public int OriginSheet2StartCol
        {
            get {
                int col;
                if (!int.TryParse(tbxOriginSheet2StartCol.Text, out col))
                    throw new Exception("請在表2開始列中填入正確的數字!");
                return col;
            }
        }
        public int OriginSheet2EndCol
        {
            get {
                int col;
                if (!int.TryParse(tbxOriginSheet2EndCol.Text, out col))
                    throw new Exception("請在表2結束列中填入正確的數字!");
                return col;
            }
        }
        public int OriginSheet2CodeCol
        {
            get {
                int col;
                if (!int.TryParse(tbxOriginSheet2CodeCol.Text, out col))
                    throw new Exception("請在表2代碼列中填入正確的數字!");
                return col;
            }
        }
        
        public string TemplatePath
        {
            get {
                if (string.IsNullOrEmpty(tbxTemplatePath.Text))
                    throw new Exception("請選擇模板文件!");
                return tbxTemplatePath.Text;
            }
        }
        
        public string TemplateSheet1Name
        {
            get {
                return tbxTemplateSheet1Name.Text;
            }
        }
        public int TemplateSheet1StartRow
        {
            get {
                int row;
                if (!int.TryParse(tbxTemplateSheet1StartRow.Text, out row))
                    throw new Exception("請在模板表1開始行中填入正確的數字!");
                return row;
            }
        }
        public int TemplateSheet1EndRow
        {
            get {
                int row;
                if (!int.TryParse(tbxTemplateSheet1EndRow.Text, out row))
                    throw new Exception("請在模板表1結束行中填入正確的數字!");
                return row;
            }
        }
        public int TemplateSheet1StartCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet1StartCol.Text, out col))
                    throw new Exception("請在模板表1開始列中填入正確的數字!");
                return col;
            }
        }
        public int TemplateSheet1EndCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet1EndCol.Text, out col))
                    throw new Exception("請在模板表1結束列中填入正確的數字!");
                return col;
            }
        }
        public int TemplateSheet1CodeCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet1CodeCol.Text, out col))
                    throw new Exception("請在模板表1代碼列中填入正確的數字!");
                return col;
            }
        }
        
        public string TemplateSheet2Name
        {
            get {
                return tbxTemplateSheet2Name.Text;
            }
        }
        public int TemplateSheet2StartRow
        {
            get {
                int row;
                if (!int.TryParse(tbxTemplateSheet2StartRow.Text, out row))
                    throw new Exception("請在模板表2開始行中填入正確的數字!");
                return row;
            }
        }
        public int TemplateSheet2EndRow
        {
            get {
                int row;
                if (!int.TryParse(tbxTemplateSheet2EndRow.Text, out row))
                    throw new Exception("請在模板表2結束行中填入正確的數字!");
                return row;
            }
        }
        public int TemplateSheet2StartCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet2StartCol.Text, out col))
                    throw new Exception("請在模板表2開始列中填入正確的數字!");
                return col;
            }
        }
        public int TemplateSheet2EndCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet2EndCol.Text, out col))
                    throw new Exception("請在模板表2結束列中填入正確的數字!");
                return col;
            }
        }
        public int TemplateSheet2CodeCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet2CodeCol.Text, out col))
                    throw new Exception("請在模板表2代碼列中填入正確的數字!");
                return col;
            }
        }
        
        public string TemplateSheet3Name
        {
            get {
                return tbxTemplateSheet3Name.Text;
            }
        }
        public int	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文翻譯自 "《Four ways to dispose IDisposables in ASP.NET Core》" ,由於水平有限,故無法保證翻譯完全正確,歡迎指出錯誤。謝謝! 介面是.NET中最常用的介面之一。當類型包含 "非托管資源的引用" .aspx),比如視窗句柄、文件或網路通信,可以實 ...
  • 首先建立Home控制器,然後再建立Index視圖。 和前面登錄頁面一樣也需要引入幾個文件。 接著就是找到你下載的easyui文件夾裡面的demo裡面的layout裡面的full 瀏覽器運行後右鍵查看代碼把裡面的body裡面的代碼複製到Index視圖的body裡面去。沒找到的可以直接複製下麵的代碼。 ...
  • 分散式架構設計之電商平臺 何為軟體架構?不同人的答案會有所不同,而我認為一個好的軟體架構除了要具備業務功能外,還應該具備一定的高性能、高可用、高伸縮性及可拓展等非功能需求。而軟體架構是由業務架構和技術架構兩部分組成,因為有了業務結構才會催生出軟體架構,進而來滿足業務上的需求,所以,在做軟體架構設計時 ...
  • 轉自 http://www.cnblogs.com/storys/archive/2013/03/06/2945914.html ...
  • 首先在資料庫中建立一張UserInfo表。 註:以下講的這些可以用動軟代碼生成器直接生成,但是對於新手來說還是動手敲一下的好,瞭解以下實現的過程。 然後在Model中建立UserInfo的實體層。 接著就在DAL層中建立UserInfo的資料庫訪問 在BLL層中建立UserInfo的邏輯處理層Use ...
  • entityFramework6在mysql資料庫下,用linq進行排序會出現一個bug. 上述代碼會報莫名其妙的nullrefrenceException 經試錯得出結論,在expression如果想用三目運算符實現動態sql,那就不能用orderBy,這兩者只能用一個 補救方案:expressi ...
  • 在發佈asp.net站點和windows服務項目時,有的時候這樣的需求:msbuild編譯之後發佈到伺服器指定目錄時要排除指定文件,比如通過jenkins構建時,不希望覆蓋原來的Web.config和App.config,那麼該如何排除呢? web站點項目 比如不希望發佈Web.config文件,有 ...
  • 在基於Metronic的Bootstrap開發框架中,一直都希望整合較新、較好的前端技術,結合MVC的後端技術進行項目的開發,隨著時間的推移,目前Metronic也更新到了4.75版本,因此著手對這個版本的內容進行一次更新調整,以期達到與時俱進的目的。從最近這幾個版本來看,Metronic本身的目錄... ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...