本文介绍了如何从sdf文件导出datagridview到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 此代码在datagridview中显示数据 private void button2_Click( object sender ,EventArgs e) { SqlCeConnection con = new SqlCeConnection( Data Source = + System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly()。Location), Database1.sdf)); con.Open(); SqlCeCommand com = con.CreateCommand(); com.CommandText =( select * from client); SqlCeDataAdapter dad = new SqlCeDataAdapter(com); DataSet ds = new DataSet(); dad.Fill(ds); if (ds.Tables [ 0 ]。Rows.Count > 0 ) { dataGridView2.DataSource = ds.Tables [ 0 ]; } } 这在导出按钮中 // 从DataTable数据创建iTextSharp表 PdfPTable pdfTable = new PdfPTable(dataGridView2.ColumnCount); pdfTable.DefaultCell.Padding = 2 ; pdfTable.WidthPercentage = 30 ; pdfTable.Horizo​​ntalAlignment = Element.ALIGN_LEFT; pdfTable.DefaultCell.BorderWidth = 1 ; // 添加标题行 foreach (DataGridViewColumn列 in dataGridView2.Columns) { PdfPCell cell = new PdfPCell( new Phrase(column.HeaderText)); cell.BackgroundColor = new iTextSharp.text.Color( 240 , 240 , 240 ); pdfTable.AddCell(cell); } // 添加DataRow foreach (DataGridViewRow row in dataGridView2.Rows) { foreach (DataGridViewCell cell in row.Cells) { pdfTable.AddCell(cell.Value.ToString ()); } } // 导出为PDF string folderPath = C:\\ \\\PDFs\\; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } 使用(FileStream stream = new FileStream(folderPath + DataGridViewExport.pdf,FileMode.Create)) { Document pdfDoc = new 文档(PageSize.A2,10f,10f,10f,0f); PdfWriter.GetInstance(pdfDoc,stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } 这是错误 对象引用未设置为对象的实例 pdftable.AddCell(Cell.Value.ToString()); 解决方案 调试并检查运行时中的null对象是什么异常出现的行,我认为 cell.Value 可能在DataGridView中为null。您可以添加以下null检查 foreach (DataGridViewCell cell in row.Cells) { if (cell.Value!= null ) { pdfTable.AddCell(cell.Value.ToString()); } else { pdfTable.AddCell( ); } } 非常感谢它的工作 foreach (DataGridViewCell cell in row.Cells) { if (cell.Value!= null ) { pdfTable.AddCell(cell.Value.ToString( )); } else { pdfTable.AddCell( ); } } 但它没有显示任何阿拉伯值 this code to show data in datagridviewprivate void button2_Click(object sender, EventArgs e) { SqlCeConnection con = new SqlCeConnection("Data Source=" + System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "Database1.sdf")); con.Open(); SqlCeCommand com = con.CreateCommand(); com.CommandText = ("select * from client"); SqlCeDataAdapter dad = new SqlCeDataAdapter(com); DataSet ds = new DataSet(); dad.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { dataGridView2.DataSource = ds.Tables[0]; } }this in export button//Creating iTextSharp Table from the DataTable data PdfPTable pdfTable = new PdfPTable(dataGridView2.ColumnCount); pdfTable.DefaultCell.Padding =2; pdfTable.WidthPercentage = 30; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; pdfTable.DefaultCell.BorderWidth = 1; //Adding Header row foreach (DataGridViewColumn column in dataGridView2.Columns) { PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText)); cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240); pdfTable.AddCell(cell); } //Adding DataRow foreach (DataGridViewRow row in dataGridView2.Rows) { foreach (DataGridViewCell cell in row.Cells) { pdfTable.AddCell(cell.Value.ToString()); } } //Exporting to PDF string folderPath = "C:\\PDFs\\"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create)) { Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); }this is the errorobject reference not set to an instance of an object"pdftable.AddCell(Cell.Value.ToString());" 解决方案 Debug and check what is the null object in the runtime, according to exception occured line, I think cell.Value may have null in your DataGridView. You can add null check as belowforeach (DataGridViewCell cell in row.Cells){ if(cell.Value!=null) { pdfTable.AddCell(cell.Value.ToString()); }else { pdfTable.AddCell(""); }}thanks alot it's workforeach (DataGridViewCell cell in row.Cells){ if(cell.Value!=null) { pdfTable.AddCell(cell.Value.ToString()); }else { pdfTable.AddCell(""); }}but it didn't show any arabic value 这篇关于如何从sdf文件导出datagridview到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 21:56