如何在datatable / dataset的指定路径创建.xls 2003文件, [英] how to create .xls 2003 file at specified path from datatable / dataset,

查看:102
本文介绍了如何在datatable / dataset的指定路径创建.xls 2003文件,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



任何人都可以帮助我从数据集/数据表创建2003版的excel(.xls)文件,而不会在打开时发出任何警告或警告消息一个文件,

我在那里使用IIS 2008 x64服​​务器我不会找到Office文件,但任务是创建的.xls文件应该在服务器中打开/工作.... br />


请帮忙,



提前致谢,





一旦我创建xls并且在打开此警报时msg不应该显示...

Hi All,

Can any1 plz help me to create excel(.xls)file of 2003 version from dataset / datatable without any warning or alert messages while opening a file,
I am using IIS 2008 x64 server there I din''t find Ms Office files, But task is that the created .xls file should open / work in server also....

please help,

Thanks in advance,


once I create xls and while opening this alert msg should not show...

Quote:

您尝试打开的文件'someFileName.xls的格式与文件扩展名指定的格式不同。验证文件是否已损坏,在打开文件之前来自可信来源。你想立即打开文件吗?

"The file you are trying to open, ''someFileName.xls, is in a different format than specified by the file extension. Verify that file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"





以及我在帮助下使用的以下代码of Microsoft.Office.Interop.Excel;

它的工作在本地主机没有任何警告/警告信息,但在服务器中它不工作Plz帮助



and the below code which I used with the help of Microsoft.Office.Interop.Excel;
Its working fine in Local Host without any warning / alert msg but in sever its not working Plz help

oleda1.Fill(ds);
 
 Microsoft.Office.Interop.Excel.Application oAppln;
//declaring work book
Microsoft.Office.Interop.Excel.Workbook oWorkBook;
//declaring worksheet
Microsoft.Office.Interop.Excel.Worksheet oWorkSheet;
oAppln = new Microsoft.Office.Interop.Excel.Application();
oWorkBook = (Microsoft.Office.Interop.Excel.Workbook)(oAppln.Workbooks.Add(true));
//oWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWorkBook.ActiveSheet;
int i2 = 0;
foreach (DataTable table in ds.Tables)
{
    //oWorkSheet = new Microsoft.Office.Interop.Excel.Worksheet();
    oWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)(oWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing));
    if (i2 == 0)
    {
        oWorkSheet.Name = "first";
    }
    else
    {
        oWorkSheet.Name = "second";
    }
    oWorkSheet.Activate();
    //oWorkBook.Worksheets.Add(null, null, 1, null);
    //DataTable table = DATASETNAME.Tables[0];
    int ColumnIndex = 0;
 
    foreach (DataColumn col in table.Columns)
    {
        ColumnIndex++;
 
        oWorkSheet.Cells[1, ColumnIndex] = col.ColumnName;
    }
    int rowIndex = 0;
    foreach (DataRow row in table.Rows)
    {
        rowIndex++;
        ColumnIndex = 0;
        foreach (DataColumn col in table.Columns)
        {
            ColumnIndex++;
            oWorkSheet.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName].ToString();
        }
    }
    // Worksheet worksheet = (Worksheet)oAppln.ActiveSheet;
    //worksheet.Activate();
    i2++;
}

推荐答案

private void button8_Click(object sender, RoutedEventArgs e)
       {
           SaveFileDialog sd = new SaveFileDialog();
           sd.Filter = "Excel File (.xls)|*.xls";
           sd.Title = "Save File";
           sd.AddExtension = true;
           sd.ShowDialog();
           if (sd.FileName != "")
           {
               OleDbConnection data = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +Wr u want save path + ";Extended Properties='Excel 8.0;HDR=Yes'");
               data.Open();
               try
               {
                   OleDbCommand cmd = new OleDbCommand("CREATE TABLE [Sheet1] ( [c1] string, [c2] string)", data);
                   cmd.ExecuteNonQuery();
                   for (int i = 0; i < yourDataset.tables[0].rows.count; i++)
                   {
                       cmd = new OleDbCommand("INSERT INTO [Sheet1] ( [c1], [c2])Values(yourDataset.tables[0].rows[i].ittemarray[0].tostring() + "','" +yourDataset.tables[0].rows[i].ittemarray[1].tostring()+ "')", data);
                       cmd.ExecuteNonQuery();
                   }
               }
               catch
               {
               }
               finally
               {
                   data.Close();
               }
           }
       }


这篇关于如何在datatable / dataset的指定路径创建.xls 2003文件,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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