尝试使用NPOI创建一个新的.xlsx文件并写入它

时间:2013-11-07 14:42:46

标签: c# excel npoi

编辑:

我正在尝试编写一个小型控制台应用程序,它将从Excel电子表格中读取行,解析行并将现场数据写入新的Excel文件。我正在使用.NET和NPOI库。我终于在apache网站上挖掘了POI的Java文档。这是我更新的代码,包含新错误。

这实际上创建了一个可读文件,除了它只将文本写入第三列。

    public static void TransferXLToTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("City", typeof(string));
        dt.Columns.Add("State", typeof(string));
        dt.Columns.Add("Zip", typeof(string));

        using (FileStream stream = new FileStream(OpenFile(), FileMode.Open, FileAccess.Read))
        {
            IWorkbook wb = new XSSFWorkbook(stream);
            ISheet sheet = wb.GetSheet("Sheet1");
            string holder;
            int i = 0;
            do
            {
                DataRow dr = dt.NewRow();
                IRow row = sheet.GetRow(i);
                try
                {
                    holder = row.GetCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
                }
                catch (Exception)
                {
                    break;
                }

                string city = holder.Substring(0, holder.IndexOf(','));
                string state = holder.Substring(holder.IndexOf(',') + 2, 2);
                string zip = holder.Substring(holder.IndexOf(',') + 5, 5);
                dr[0] = city;
                dr[1] = state;
                dr[2] = zip;
                dt.Rows.Add(dr);
                i++;
            } while (!String.IsNullOrEmpty(holder));
        }

        using (FileStream stream = new FileStream(@"C:\Working\FieldedAddresses.xlsx", FileMode.Create, FileAccess.Write))
        {
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("Sheet1");
            ICreationHelper cH = wb.GetCreationHelper();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    IRow row = sheet.CreateRow(i);
                    ICell cell = row.CreateCell(j);
                    cell.SetCellValue(cH.CreateRichTextString(dt.Rows[i].ItemArray[j].ToString()));
                }
            }
            wb.Write(stream);
        }
    }

1 个答案:

答案 0 :(得分:24)

我弄清楚出了什么问题。这实际上是一个非常简单的错误,当我需要在外部循环中创建行时,我正在最内部的for循环中创建一个新行。希望答案中的工作代码对于需要NPOI起点的任何人都有用。

对于任何刚开始使用POI或NPOI的人来说,本网站都是一个很好的资源。

http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook

这是工作代码。

    public static void TransferXLToTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("City", typeof(string));
        dt.Columns.Add("State", typeof(string));
        dt.Columns.Add("Zip", typeof(string));

        using (FileStream stream = new FileStream(OpenFile(), FileMode.Open, FileAccess.Read))
        {
            IWorkbook wb = new XSSFWorkbook(stream);
            ISheet sheet = wb.GetSheet("Sheet1");
            string holder;
            int i = 0;
            do
            {
                DataRow dr = dt.NewRow();
                IRow row = sheet.GetRow(i);
                try
                {
                    holder = row.GetCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
                }
                catch (Exception)
                {
                    break;
                }

                string city = holder.Substring(0, holder.IndexOf(','));
                string state = holder.Substring(holder.IndexOf(',') + 2, 2);
                string zip = holder.Substring(holder.IndexOf(',') + 5, 5);
                dr[0] = city;
                dr[1] = state;
                dr[2] = zip;
                dt.Rows.Add(dr);
                i++;
            } while (!String.IsNullOrEmpty(holder));
        }

        using (FileStream stream = new FileStream(@"C:\Working\FieldedAddresses.xlsx", FileMode.Create, FileAccess.Write))
        {
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("Sheet1");
            ICreationHelper cH = wb.GetCreationHelper();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row = sheet.CreateRow(i);
                for (int j = 0; j < 3; j++)
                {
                    ICell cell = row.CreateCell(j);
                    cell.SetCellValue(cH.CreateRichTextString(dt.Rows[i].ItemArray[j].ToString()));
                }
            }
            wb.Write(stream);
        }
    }