x01.ExcelHelper: NPOI 操作

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 TemplateSheet3StartRow
        {
            get {
                int row;
                if (!int.TryParse(tbxTemplateSheet3StartRow.Text, out row))
                    throw new Exception("请在模板表3开始行中填入正确的数字!");
                return row;
            }
        }
        public int TemplateSheet3EndRow
        {
            get {
                int row;
                if (!int.TryParse(tbxTemplateSheet3EndRow.Text, out row))
                    throw new Exception("请在模板表3结束行中填入正确的数字!");
                return row;
            }
        }
        public int TemplateSheet3StartCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet3StartCol.Text, out col))
                    throw new Exception("请在模板表3开始列中填入正确的数字!");
                return col;
            }
        }
        public int TemplateSheet3EndCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet3EndCol.Text, out col))
                    throw new Exception("请在模板表3结束列中填入正确的数字!");
                return col;
            }
        }
        public int TemplateSheet3CodeCol
        {
            get {
                int col;
                if (!int.TryParse(tbxTemplateSheet3CodeCol.Text, out col))
                    throw new Exception("请在模板表3代码列中填入正确的数字!");
                return col;
            }
        }

        #endregion

        OpenFileDialog openDialog = new OpenFileDialog();
        SaveFileDialog saveDialog = new SaveFileDialog();
        public SplitWindow()
        {
            InitializeComponent();

            openDialog.Filter = "Excel Files(*.xls)|*.xls|All Files(*.*)|*.*";
            saveDialog.Filter = "Excel Files(*.xls)|*.xls|All Files(*.*)|*.*";
        }

        void OpenTemplateButton_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)openDialog.ShowDialog()) {
                tbxTemplatePath.Text = openDialog.FileName;
            }
        }

        void OpenOriginButton_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)openDialog.ShowDialog()) {
                tbxOriginPath.Text = openDialog.FileName;
            }
        }

        void GenerateFilesButton_Click(object sender, RoutedEventArgs e)
        {
            var orgBook = CreateWorkook(OriginPath);
            var orgSheet1 = GetSheet(orgBook,OriginSheet1Name);
            var orgSheet2 = GetSheet(orgBook,OriginSheet2Name);

            var tempBook = CreateWorkook(TemplatePath);
            var tempSheet1 = GetSheet(tempBook,TemplateSheet1Name);
            var tempSheet2 = GetSheet(tempBook,TemplateSheet2Name);
            var tempSheet3 = GetSheet(tempBook, TemplateSheet3Name);

            if (orgSheet1 != null) {
                for (int j = OriginSheet1StartCol - 1; j < OriginSheet1EndCol; j++) {
                    string name = orgSheet1.GetRow(OriginSheet1StartRow-1).GetCell(j).StringCellValue;
                    GenerateTemplateSheet(ref orgSheet1, ref tempSheet1, j, name,
                                          OriginSheet1StartRow, OriginSheet1EndRow, OriginSheet1CodeCol,
                                         TemplateSheet1StartRow, TemplateSheet1EndRow,
                                         TemplateSheet1StartCol, TemplateSheet1EndCol, TemplateSheet1CodeCol);
                    GenerateTemplateSheet(ref orgSheet1, ref tempSheet2, j, name,
                                          OriginSheet1StartRow, OriginSheet1EndRow, OriginSheet1CodeCol,
                                         TemplateSheet2StartRow, TemplateSheet2EndRow,
                                         TemplateSheet2StartCol, TemplateSheet2EndCol, TemplateSheet2CodeCol);
                    GenerateTemplateSheet(ref orgSheet1, ref tempSheet3, j, name,
                                          OriginSheet1StartRow, OriginSheet1EndRow, OriginSheet1CodeCol,
                                         TemplateSheet3StartRow, TemplateSheet3EndRow,
                                         TemplateSheet3StartCol, TemplateSheet3EndCol, TemplateSheet3CodeCol);
                    var fs = new FileStream(Path.Combine(Path.GetDirectoryName(TemplatePath),name+".xls"), FileMode.Create);
                    tempBook.Write(fs);
                    fs.Close();
                    tempBook = CreateWorkook(TemplatePath);
                    tempSheet1 = GetSheet(tempBook, TemplateSheet1Name);
                    tempSheet2 = GetSheet(tempBook, TemplateSheet2Name);
                    tempSheet3 = GetSheet(tempBook, TemplateSheet3Name);
                }
            }
            if (orgSheet2 != null) {
                for (int j = OriginSheet2StartCol - 1; j < OriginSheet2EndCol; j++) {
                    string name = orgSheet2.GetRow(OriginSheet1StartRow-1).GetCell(j).StringCellValue;
                    GenerateTemplateSheet(ref orgSheet2, ref tempSheet1, j, name,
                                          OriginSheet2StartRow, OriginSheet2EndRow, OriginSheet2CodeCol,
                                         TemplateSheet1StartRow, TemplateSheet1EndRow,
                                         TemplateSheet1StartCol, TemplateSheet1EndCol, TemplateSheet1CodeCol);
                    GenerateTemplateSheet(ref orgSheet2, ref tempSheet2, j, name,
                                          OriginSheet2StartRow, OriginSheet2EndRow, OriginSheet2CodeCol,
                                         TemplateSheet2StartRow, TemplateSheet2EndRow,
                                         TemplateSheet2StartCol, TemplateSheet2EndCol, TemplateSheet2CodeCol);
                    GenerateTemplateSheet(ref orgSheet2, ref tempSheet3, j, name,
                                          OriginSheet2StartRow, OriginSheet2EndRow, OriginSheet2CodeCol,
                                         TemplateSheet3StartRow, TemplateSheet3EndRow,
                                         TemplateSheet3StartCol, TemplateSheet3EndCol, TemplateSheet3CodeCol);
                    var fs = new FileStream(Path.Combine(Path.GetDirectoryName(TemplatePath),name+".xls"), FileMode.Create);
                    tempBook.Write(fs);
                    fs.Close();
                    tempBook = CreateWorkook(TemplatePath);
                    tempSheet1 = GetSheet(tempBook, TemplateSheet1Name);
                    tempSheet2 = GetSheet(tempBook, TemplateSheet2Name);
                    tempSheet3 = GetSheet(tempBook, TemplateSheet3Name);
                }
            }

            MessageBox.Show("OK!");
        }

        void GenerateTemplateSheet(ref ISheet orgSheet, ref ISheet tempSheet,
                                   int orgCol, string name,
                                   int orgStartRow, int orgEndRow, int orgCodeCol,
                                   int tempStartRow, int tempEndRow,
                                   int tempStartCol, int tempEndCol, int tempCodeCol)
        {
            for (int i = orgStartRow; i < orgEndRow; i++) {
                if (tempSheet != null) {
                    for (int y = tempStartRow - 1; y < tempEndRow; y++) {
                        for (int x = tempStartCol - 1; x < tempEndCol; x++) {
                            if (tempSheet.GetRow(y).GetCell(tempCodeCol - 1).NumericCellValue
                                == orgSheet.GetRow(i).GetCell(orgCodeCol - 1).NumericCellValue)
                            {
                                tempSheet.GetRow(y).GetCell(x).SetCellValue(orgSheet.GetRow(i).GetCell(orgCol).NumericCellValue);
                            }
                        }
                    }
                }
            }
            for (int y = tempStartRow - 1; y < tempEndRow; y++) {
                if (tempSheet == null)
                    break;
                tempSheet.GetRow(y).GetCell(tempCodeCol - 1).SetCellType(CellType.Blank);
            }
        }

        HSSFWorkbook CreateWorkook(string path)
        {
            var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            var book = new HSSFWorkbook(fs);
            fs.Close();
            return book;
        }

        ISheet GetSheet(HSSFWorkbook book, string sheetName)
        {
            if (string.IsNullOrEmpty(sheetName))
                return null;
            return book.GetSheet(sheetName);
        }

    }
}

SplitWindow.xaml.cs

运行填充相关设置后,点击生成按钮,瞬间完成,OK!

时间: 2024-11-05 12:15:42

x01.ExcelHelper: NPOI 操作的相关文章

使用 WeihanLi.Npoi 操作 CSV

使用 WeihanLi.Npoi 操作 CSV Intro 最近发现 csv 文件在很多情况下都在使用,而且经过大致了解,csv 格式简单,相比 excel 文件要小很多,读取也很是方便,而且也很通用,微软的 ml.net 的示例项目 用来训练模型的数据也是使用的 csv 来保存的,最近又发现使用 jmeter 来测试网站的性能,也可以用 csv 来参数化请求,csv 文件操作的重要性由此可见. 此前做了一个 NPOI 的扩展 WeihanLi.Npoi,支持.net45 以及 .netstan

NPOI操作Excel 003:写入空Excel

对于NPOI操作Excel前面已经有了简单认识(http://blog.csdn.net/yysyangyangyangshan/article/details/42614209).继续来看如何将内容保存至Excel中.根据前面的经验NPOI操作Excel主要的几个对象分别是:workbook,sheet以及sheet内的row和cell.所以保存至Excel也是对这几个对象进行操作.当然我们平时使用Excel时不光要在单元格中保存内容,还要设置单元格的格式以及字体大小等,也就是格式和样式.这些

NPOI操作excel——利用反射机制,NPOI读取excel数据准确映射到数据库字段

> 其实需求很明确,就是一大堆不一样的excel,每张excel对应数据库的一张表,我们需要提供用户上传excel,我们解析数据入库的功能实现. 那么,这就涉及到一个问题:我们可以读出excel的表头,但是怎么知道每个表头具体对应数据库里面的字段呢? 博主经过一段时间的思考与构思,想到一法:现在的情况是我们有excel表A,对应数据库表B,但是A与B具体属性字段的映射关系我们不知.那我们是不是可以有一个A到B的映射文件C呢? 我想,说到这,大家就很明了了... 第一步:为每张excel创建一个与

数据字典生成工具之旅(4):NPOI操作EXECL

这篇会介绍NPOI读写EXECL,读写EXECL的组件很多,可以使用微软自己的COM组件EXECL.exe读写,不过这种方式限制很大. 1:客户环境必须装Office(虽然现在机子上不装Office的几乎没有吧) 2:EXECL读写完毕后EXECL还有进程还留在后台  ,内存回收不了 基于以上的原因,就采用了开源组件NPOI进行操作了. 阅读目录 NPOI简介 简单示例 NPOI在本工具的使用及总结 工具源代码下载 学习使用 回到顶部 NPOI简介 1.Excel表格叫做工作表:WorkBook

Npoi操作excel

转载地址:http://www.cnblogs.com/knowledgesea/archive/2012/11/16/2772547.html Npoi操作excel Npoi 简介 1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet:行:Row:单元格Cell. 2.Npoi 下载地址:http://npoi.codeplex.com/releases/view/38113 3.Npoi 学习系列教程推荐:http://www.cnblogs.co

用NPOI操作EXCEL-锁定列CreateFreezePane()

public void ExportPermissionRoleData(string search, int roleStatus) { var workbook = new HSSFWorkbook(); string random = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(100); string fileName = HttpUtility.UrlEncode("sheet"

C#用NPOI操作读写2007版的excel所走的弯路

C#用NPOI操作读写类网上很多了,在此不重复. 我引用的dll:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll, 一样的代码操作2003版的excel没有问题,在操作2007版的excel时出现莫名其妙的问题,而且catch不到异常信息. 解决方法:需引用ICSharpCode.SharpZipLib.dll. So需要引用的dll,有四个:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode

NPOI操作EXCEL--设置密码及设置只读

有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完成,如下图:      那么,在NPOI中有没有办法通过编码的方式达到这一效果呢?答案是肯定的. HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); HSSFRow row1 = sheet1.CreateRow(0);HSSFCell cel1 = row1.CreateCell(0);HS

C#开发中使用Npoi操作excel实例代码

C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet:行:Row:单元格Cell. 2.Npoi 下载地址:http://npoi.codeplex.com/releases/view/38113 3.Npoi 学习系列教程推荐:http://www.cnblogs.com