如何从sdf文件导出datagridview到excel [英] how to export datagridview to excel from sdf file

查看:122
本文介绍了如何从sdf文件导出datagridview到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在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
< span class =code-keyword> 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 < span class =code-keyword> 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 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];

            }
        }


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 error
object 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 below

foreach (DataGridViewCell cell in row.Cells)
{
   if(cell.Value!=null)
   {
      pdfTable.AddCell(cell.Value.ToString());
   }else
   {
      pdfTable.AddCell("");
   }
}


thanks alot it's work

foreach (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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆