Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Office Open XMl SDK将数字写入工作表_C#_Excel_Openxml Sdk - Fatal编程技术网

C# Office Open XMl SDK将数字写入工作表

C# Office Open XMl SDK将数字写入工作表,c#,excel,openxml-sdk,C#,Excel,Openxml Sdk,我正在尝试将数字从数据表写入数据表-不幸的是,这并不像预期的那样有效,例如。G数据表已损坏 我正在使用以下代码: private void AddDataToSheet(ExcelViewData data, SheetData sheetData) { var excelData = data.WriteableDataTable; //// this returns a datatable ////the numbers ha

我正在尝试将数字从数据表写入数据表-不幸的是,这并不像预期的那样有效,例如。G数据表已损坏

我正在使用以下代码:

    private void AddDataToSheet(ExcelViewData data, SheetData sheetData)
            {
                var excelData = data.WriteableDataTable; 
//// this returns a datatable
////the numbers have a format like "8,1" "8,0" etc.
                for (int i = 0; i < excelData.Rows.Count; i++)
                {
                    Row row = new Row();
                    //row.RowIndex = (UInt32)i;
                    for (int c = 0; c < excelData.Columns.Count; c++)
                    {
                        Cell cell = new Cell();
                        CellValue cellvalue = new CellValue();
                        //cell.CellReference = SharedMethods.GetExcelColumnName(i + 1) + (c + 1).ToString();
                        cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number;
                        cellvalue.Text = excelData.Rows[i][c].ToString().Replace(",",".");
                        cell.Append(cellvalue);
                        row.Append(cell);
                    }

                    sheetData.Append(row);
                }
            }
private void AddDataToSheet(ExcelViewData数据,SheetData SheetData)
{
var excelData=data.WriteableDataTable;
////这将返回一个数据表
////这些数字的格式类似于“8,1”、“8,0”等。
对于(int i=0;i
知道为什么会失败吗?我已经找到了多个使用相同方法的教程。

尝试以下方法:

public void InsertDataTableIntoExcel(SpreadsheetDocument _excelDoc, SheetData    SheetData,  DataTable excelData, int rowIndex = 1)
    {
        if (_excelDoc != null && SheetData != null)
        {
            if (excelData.Rows.Count > 0)
            {
                try
                {
                    uint lastRowIndex = (uint)rowIndex;
                    for (int row = 0; row < excelData.Rows.Count; row++)
                    {
                        Row dataRow = GetRow(lastRowIndex, true);
                        for (int col = 0; col < excelData.Columns.Count; col++)
                        {
                            Cell cell = GetCell(dataRow, col + 1, lastRowIndex);

                            string objDataType = excelData.Rows[row][col].GetType().ToString();
                            //Add text to text cell
                            if (objDataType.Contains(TypeCode.Int32.ToString()) || objDataType.Contains(TypeCode.Int64.ToString()) || objDataType.Contains(TypeCode.Decimal.ToString()))
                            {
                                cell.DataType = new EnumValue<CellValues>(CellValues.Number);
                                cell.CellValue = new CellValue(objData.ToString());
                            }
                            else
                            {
                                cell.CellValue = new CellValue(objData.ToString());
                                cell.DataType = new EnumValue<CellValues>(CellValues.String);
                            }
                        }
                        lastRowIndex++;
                    }
                }
                catch (OpenXmlPackageException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                OpenXmlPackageException openEx = new OpenXmlPackageException("No data from datatable");
                throw openEx;
            }
        }
        else
        {
            OpenXmlPackageException openEx = new OpenXmlPackageException("Workbook not found");
            throw openEx;
        }
    }
public void InsertDataTableIntoExcel(电子表格文档excelDoc,SheetData SheetData,DataTable excelData,int rowIndex=1)
{
如果(_excelDoc!=null&&SheetData!=null)
{
如果(excelData.Rows.Count>0)
{
尝试
{
uint lastRowIndex=(uint)rowIndex;
对于(int row=0;row
谢谢你的代码-不幸的是,我从一张完全空白的表格开始,没有行可写。我将尝试内部代码-它看起来非常有前途!这是一个新的方法,工作得很好,你可以利用你创建单元格的代码,添加单元格值和单元格类型。这只是单元格的格式。我希望这能给你答案。