Trying to create a new .xlsx file using NPOI and write to it

两盒软妹~` 提交于 2019-12-17 22:35:55

问题


Edit:

I'm trying to write a small console application that will read in lines from an excel spreadsheet, parse the lines and write the fielded data to a new excel file. I'm using .NET and the NPOI library. I finally after much digging found Java documentation for POI on the apache site. Here is my updated code with new errors.

This actually creates a readable file except it only writes text to the third column.

    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:


I figured out what was wrong. It was actually a very simple mistake, I was creating a new row in the inner most for loop when I needed to create the row in the outer loop. Hopefully the working code in the answer will be useful for anyone who needs a starting point with NPOI.

This website is a great resource for anyone starting out with POI or NPOI.

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

Here is working code.

    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);
        }
    }


来源:https://stackoverflow.com/questions/19838743/trying-to-create-a-new-xlsx-file-using-npoi-and-write-to-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!