简体   繁体   English

C#导出到Excel并给出十六进制值0x02``,这是无效的字符错误

[英]C# Export to Excel giving '', hexadecimal value 0x02, is an invalid character error

giving '', hexadecimal value 0x02, is an invalid character error 给出'',十六进制值为0x02,是无效的字符错误

foreach (DataRow dataRow in data.Rows)
    {
         row = new DocumentFormat.OpenXml.Spreadsheet.Row { RowIndex =+rowcount};
         for (int i = 0; i < fieldsToExpose.Length; i++)
            {
               row.Append(CreateTextCell(ColumnLetter(i), rowcount,dataRow[fieldsToExpose[i]].ToString()));
            }
             sheetData.AppendChild(row);
     }
 worksheetPart.Worksheet.Save();

Getting error when saving the worksheetPart.Worksheet.Save() because 'row' contains the hexadecimal value 保存worksheetPart.Worksheet.Save()时出错,因为“行”包含十六进制值

    //Try some thing like this... You can export your entire dataGridView through this.  

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Configuration;


            void ExportTOExcel(DataGridView gridviewID)
            {


                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;

                xlApp = new Microsoft.Office.Interop.Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                //add data 
                int StartCol = 1;`enter code here`
                int StartRow = 1;
                int j = 0, i = 0;

                //Write Headers
                for (j = 0; j < gridviewID.Columns.Count; j++)
                {
                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[StartRow, StartCol + j];
                    myRange.Value2 = gridviewID.Columns[j].HeaderText;
                }

                StartRow++;

                //Write datagridview content
                for (i = 0; i < gridviewID.Rows.Count; i++)
                {
                    for (j = 0; j < gridviewID.Columns.Count; j++)
                    {
                        try
                        {
                            Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[StartRow + i, StartCol + j];
                            myRange.Value2 = gridviewID[j, i].Value == null ? "" : gridviewID[j, i].Value;
                        }
                        catch
                        {
                            ;
                        }
                    }
                }

                Microsoft.Office.Interop.Excel.Range chartRange;

                Microsoft.Office.Interop.Excel.ChartObjects xlCharts = (Microsoft.Office.Interop.Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
                Microsoft.Office.Interop.Excel.ChartObject myChart = (Microsoft.Office.Interop.Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
                Microsoft.Office.Interop.Excel.Chart chartPage = myChart.Chart;

                chartRange = xlWorkSheet.get_Range("A1", "B" + gridviewID.Rows.Count);
                chartPage.SetSourceData(chartRange, misValue);
                chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnClustered;

                xlApp.Visible = true;

            }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM