无法通过OLEDB连接将Excel转换为PDF [英] Unable to Convert Excel to PDF through OLEDB connection

查看:81
本文介绍了无法通过OLEDB连接将Excel转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft Office Access数据库引擎找不到对象"Sample"。确保对象存在,并正确拼写其名称和路径名。


当我尝试将excel转换为pdf时,我得到的上述错误.....所以可以一请说出这个错误


谢谢和问候,


Surakshith




解决方案

嗨Surakshith Reddy,


根据对于您的描述和相关的错误消息,我不确定是什么原因造成的问题,请您分享一些有关该问题的信息,例如相关代码,可生成的步骤。


In另外,这里是一个示例,它通过ocelb从excel将记录加载到数据表中,并将数据表转换为PDF供您参考。

 static void Main(string [] args)
{

try
{
string connstr = @" Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\ excelpath \ test3。 XLSX;扩展属性='Excel 12.0 Xml; HDR = YES';" ;;


DataTable dt = new DataTable();
using(var conn = new OleDbConnection(connstr))
{
string sql = @" select * from [Sheet1


" ;;
OleDbCommand command = new OleDbCommand(sql,conn);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);

string filePath = @" D:\ savepath\sample.pdf" ;;
adapter.Fill(dt);
ExportToPdf(dt,filePath);
}



Console.ReadLine();

}
catch(exception ex)
{

throw;
}
}


public static void ExportToPdf(DataTable dt,string filePath)
{
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document,new FileStream(filePath,FileMode.Create));
document.Open();
iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA,5);

PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
// float [] widths = new float [] {4f,4f,4f,4f};

//table.SetWidths (widths);

table.WidthPercentage = 100;
PdfPCell cell = new PdfPCell(new Phrase("Products"));

cell.Colspan = dt.Columns.Count;

foreach(dt.Columns中的DataColumn c)
{

table.AddCell(new Phrase(c.ColumnName,font5));
}

foreach(DataRow r in dt.Rows)
{
if(dt.Rows.Count> 0)
{
for(int j = 0; j< dt.Columns.Count; j ++)
{
table.AddCell(new Phrase(r [j] .ToString(),font5));
}
}
}
document.Add(table);
document.Close();
}


祝你好运,


章龙


The Microsoft Office Access database engine could not find the object 'Sample'. Make sure the object exists and that you spell its name and the path name correctly.

The above error I am getting when I trying to convert excel to pdf.....So could any one please say about this error

Thanks and Regards,

Surakshith


解决方案

Hi Surakshith Reddy,

According to your description and related error message, I am not sure what cause the issue, could you please share a bit more information about the issue, such as related code, the producible steps.

In addition, here is sample that load the records into datatable via oledb from excel and convert datatable to PDF for your reference.

static void Main(string[] args)
        {

            try
            {
                string connstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\excelpath\test3.xlsx; Extended Properties='Excel 12.0 Xml; HDR = YES';";

               
                DataTable dt = new DataTable();
                using (var conn = new OleDbConnection(connstr))
                {
                    string sql = @"select * from [Sheet1


"; OleDbCommand command = new OleDbCommand(sql, conn); conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(command); string filePath = @"D:\savepath\sample.pdf"; adapter.Fill(dt); ExportToPdf(dt, filePath); } Console.ReadLine(); } catch (Exception ex) { throw; } } public static void ExportToPdf(DataTable dt, string filePath) { Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)); document.Open(); iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5); PdfPTable table = new PdfPTable(dt.Columns.Count); PdfPRow row = null; //float[] widths = new float[] { 4f, 4f, 4f, 4f }; //table.SetWidths(widths); table.WidthPercentage = 100; PdfPCell cell = new PdfPCell(new Phrase("Products")); cell.Colspan = dt.Columns.Count; foreach (DataColumn c in dt.Columns) { table.AddCell(new Phrase(c.ColumnName, font5)); } foreach (DataRow r in dt.Rows) { if (dt.Rows.Count > 0) { for(int j = 0; j< dt.Columns.Count; j++) { table.AddCell(new Phrase(r[j].ToString(), font5)); } } } document.Add(table); document.Close(); }

Best regards,

Zhanglong


这篇关于无法通过OLEDB连接将Excel转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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