Exemple #1
0
        // parameters:
        //      nRowIndex   行号。从 0 开始计算
        public void WriteExcelLine(
            int nRowIndex,
            List <CellData> cells,
            WriteExcelLineStyle style = WriteExcelLineStyle.AutoString)
        {
            // 创建一个 Row。不负责查重
            // row 的 cell 创建完成后,记得 ws.Save()
            Row row = ExcelUtil.CreateRow(
                worksheetPart.Worksheet,
                (UInt32)(nRowIndex + 1));

            foreach (CellData data in cells)
            {
                string strCellName = GetColumnName(data.Col) + (nRowIndex + 1).ToString();

                // 追加一个 Cell
                DocumentFormat.OpenXml.Spreadsheet.Cell cell = ExcelUtil.AppendCell(
                    row,
                    strCellName);


                bool isString = data.IsString;
                if ((style & WriteExcelLineStyle.AutoString) == WriteExcelLineStyle.AutoString)
                {
                    isString = !IsExcelNumber(data.Value);
                }

                if (isString)
                {
                    if ((style & WriteExcelLineStyle.ShareString) == WriteExcelLineStyle.ShareString)
                    {
                        // Either retrieve the index of an existing string,
                        // or insert the string into the shared string table
                        // and get the index of the new item.
                        int stringIndex = ExcelUtil.InsertSharedStringItem(this.workbookpart, data.Value);
                        cell.CellValue = new CellValue(stringIndex.ToString());
                        cell.DataType  = new EnumValue <CellValues>(CellValues.SharedString);
                    }
                    else
                    {
                        cell.CellValue = new CellValue(data.Value);
                        cell.DataType  = new EnumValue <CellValues>(CellValues.String);
                    }
                }
                else
                {
                    cell.CellValue = new CellValue(data.Value);
                    cell.DataType  = new EnumValue <CellValues>(CellValues.Number);
                }

                if (data.StyleIndex > 0)
                {
                    cell.StyleIndex = data.StyleIndex;
                }
            }
        }
Exemple #2
0
        // parameters:
        //      nRowIndex   行号。从 0 开始计算
        public void WriteExcelLine(
            int nRowIndex,
            List<CellData> cells,
            WriteExcelLineStyle style = WriteExcelLineStyle.AutoString)
        {
            // 创建一个 Row。不负责查重
            // row 的 cell 创建完成后,记得 ws.Save()
            Row row = ExcelUtil.CreateRow(
                worksheetPart.Worksheet,
                (UInt32)(nRowIndex + 1));

            foreach (CellData data in cells)
            {
                string strCellName = GetColumnName(data.Col) + (nRowIndex + 1).ToString();

                // 追加一个 Cell
                DocumentFormat.OpenXml.Spreadsheet.Cell cell = ExcelUtil.AppendCell(
                    row,
                    strCellName);


                bool isString = data.IsString;
                if ((style & WriteExcelLineStyle.AutoString) == WriteExcelLineStyle.AutoString)
                    isString = !IsExcelNumber(data.Value);

                if (isString)
                {
                    if ((style & WriteExcelLineStyle.ShareString) == WriteExcelLineStyle.ShareString)
                    {
                        // Either retrieve the index of an existing string,
                        // or insert the string into the shared string table
                        // and get the index of the new item.
                        int stringIndex = ExcelUtil.InsertSharedStringItem(this.workbookpart, data.Value);
                        cell.CellValue = new CellValue(stringIndex.ToString());
                        cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
                    }
                    else
                    {
                        cell.CellValue = new CellValue(data.Value);
                        cell.DataType = new EnumValue<CellValues>(CellValues.String);
                    }
                }
                else
                {
                    cell.CellValue = new CellValue(data.Value);
                    cell.DataType = new EnumValue<CellValues>(CellValues.Number);
                }

                if (data.StyleIndex > 0)
                    cell.StyleIndex = data.StyleIndex;
            }
        }