導航:首頁 > 文件教程 > datagridview導出word

datagridview導出word

發布時間:2021-12-05 00:40:31

㈠ c#窗體DataGridView導出word出現問題 異常:數字必須介於 1 和 63 之間

自建了一個dataGridView,然後運行時你的源碼也出現了一些問題,改了部分代碼運行能成功導出,但是當你導出的列很多時(我嘗試導出20多列的數據)導出的效果很難看,這是排版問題,樓主看一下吧!看在你電腦上運行怎樣。

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;

{
publicpartialclassForm4:Form
{publicForm4(){InitializeComponent();}
publicvoidExportDataGridView(DataGridViewsrcDgv)
{SaveFileDialogsfile=newSaveFileDialog();
sfile.AddExtension=true;
objectVisible=true;//問題就在這里,改成true後就能正常運行了
sfile.DefaultExt=".doc";
sfile.Filter="(*.doc)|*.doc";
if(sfile.ShowDialog()==DialogResult.OK)
{
objectpath=sfile.FileName;
Objectnone=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationwordApp=newMicrosoft.Office.Interop.Word.Application();
wordApp.DisplayAlerts=Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
Microsoft.Office.Interop.Word.Documentdocument=wordApp.Documents.Add(refnone,refnone,refnone,refnone);
wordApp.Visible=true;
document.Activate();
//建立表格
try
{
Microsoft.Office.Interop.Word.Tabletable=document.Tables.Add(document.Paragraphs.Last.Range,srcDgv.Rows.Count,srcDgv.Columns.Count,refnone,refnone);
for(inti=0;i<srcDgv.Columns.Count;i++)//設置標題
{
table.Cell(0,i+1).Range.Text=srcDgv.Columns[i].HeaderText;
}
for(inti=1;i<srcDgv.Rows.Count;i++)//填充數據
{
for(intj=0;j<srcDgv.Columns.Count;j++)
{
table.Cell(i+1,j+1).Range.Text=srcDgv[j,i-1].Value.ToString();
}
}
document.SaveAs(refpath,refnone,refnone,refnone,refnone,refnone,refnone,refnone,refnone,refnone,refnone,refnone,refnone,refnone,refnone,refnone);
document.Close(refVisible,refnone,refnone);
MessageBox.Show("導出成功!");
}
finally
{
wordApp.Quit(refVisible,refnone,refnone);//最好把這行注釋,我運行時會出現在「RPC伺服器不可用」錯誤
}}}
privatevoidbutton1_Click(objectsender,EventArgse)
{
ExportDataGridView(dataGridView1);
}}}

㈡ 如何實現DataGridView導出CSV和XLS文件

SQLServer表數據導出為Excel文件
一、少量數據導出
1、在查詢分析器中,使用SQL語句檢索要導出的數據
2、選中所有數據,點擊滑鼠右鍵,選擇另存為CSV文件
3、用EXCEL把CSV文件另存為xls文件。

二、表格批量導出

1、開始菜單選擇"導入和導出數據"

2、選擇要導出的資料庫,如圖配置

3、設置導出目標

4、設置目標文件

5、選擇篩選數據發送

6、選擇導出的表格

7、完成設置

㈢ 如何將.net中DataGridView中的數據導出到Excel文本(是在windows窗體中)

private void button2_Click(object sender, EventArgs e)

{

string strconn = "Data Source=JUKINB-SERVER;Initial Catalog=TxData;Persist Security Info=True;User ID=sa";

SqlConnection conn = new SqlConnection(strconn);

string strcom = "select * from fzje";

SqlDataAdapter sqlda = new SqlDataAdapter(strcom, conn);

DataSet sqlds = new DataSet();

sqlda.Fill(sqlds, "table1");

sqlds.Tables["table1"].AcceptChanges();

this.Cursor = Cursors.WaitCursor;

Excel.Application myExcel = new Excel.Application();

myExcel.Application.Workbooks.Add(true);

myExcel.Visible = true;

//Object Date[ds.Tables["CurrentTable"].Rows.Count][ds.Tables["CurrentTable"].Columns.Count];

//生成標題

for (int i = 0; i < sqlds.Tables["table1"].Columns.Count; i++)

{

myExcel.Cells[2, i + 1] = sqlds.Tables["table1"].Columns[i].Caption;

}

//填充數據

for (int i = 0; i < sqlds.Tables["table1"].Rows.Count; i++)

{

for (int j = 0; j < sqlds.Tables["table1"].Columns.Count; j++)

{

if (sqlds.Tables["table1"].Rows[i][j].GetType() == typeof(System.String))

{

myExcel.Cells[i + 3, j + 1] = "'" + sqlds.Tables["table1"].Rows[i][j].ToString();

}

else

{

myExcel.Cells[i + 3, j + 1] = sqlds.Tables["table1"].Rows[i][j].ToString();

}

}

}

this.Cursor = Cursors.Default;

MessageBox.Show("導出成功! ", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

}

}
連接字元串自己改一下,這個方法可能速度不是很高!

㈣ c#如何把datagridview導出到excel

1.先去導入dll庫:Microsoft.Office.Interop.Excel.dl

㈤ c# 中 dataGridView中的導出與列印

命名空間。。。
using System.IO;
using System.Net.Mime;
給你導出的代碼。。
//導出數據
protected void Button1_Click(object sender, EventArgs e)
{ Export("application/ms-excel", "訂單信息.xls", GridView2);
}
public void Export(string FileType, string FileName, GridView gv)
{
Response.Charset = "big5";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//顏色和字體隨你自己願意
gv.BackColor = System.Drawing.Color.White;
gv.ForeColor = System.Drawing.Color.Black;
gv.RowStyle.BackColor = System.Drawing.Color.White;
gv.RowStyle.ForeColor = System.Drawing.Color.Black;
gv.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
gv.AlternatingRowStyle.ForeColor = System.Drawing.Color.Black;
gv.BorderStyle = BorderStyle.Solid;
gv.BorderWidth = 1;
gv.HeaderStyle.BackColor = System.Drawing.Color.White;
gv.HeaderStyle.ForeColor = System.Drawing.Color.Black;
gv.AllowPaging = false;
gv.AllowSorting = false;
gv.Columns[0].Visible = false;
gv.Columns[7].Visible = false;
gv.Columns[9].Visible = false;// 這一列不顯示
gv.DataBind();
gv.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

gv.AllowPaging = true;
gv.DataBind();

}//下面這個必須要
public override void VerifyRenderingInServerForm(Control control)
{
}

㈥ vb.net dataGridView導出EXCEL

你的參數沒用對,row 沒加1,修改後的代碼如下(我已成功導出,注意datagridview1的參數。):

'//記錄內容
row = 2
' col = 1
For i = 0 To DataGridView1.RowCount - 1
col = 1
For j = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Columns(j).Visible Then
'xlSheet.Cells(i + 2, col) = DataGridView1.Rows(i).Cells(j).Value
xlSheet.Cells(row, col) = DataGridView1(j, i).Value.ToString
col = col + 1
End If
Next
row = row + 1
Next
xlSheet.Visible = True

㈦ C# 將dataGriwView 數據導出word或者excel,求導出的方法體,最好導出為表格。 求方法體代碼。

我給你一個導出成excel的吧

namespace EXCELExport
{
public class DgvExport
{
#region 變數聲明
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern int GetWindowThreadProcessId(IntPtr hwad, out int id); //獲取excel進程ID號 導出結束進程使用
private string m_FileName = DateTime.Today.ToString("yyyy-MM-dd"); //導出文件名
private DataTable dt = new DataTable();

#endregion

#region 屬性定義

/// <summary>
/// 導出文件名
/// </summary>
public string FileName
{
get
{
return m_FileName;
}
set
{
m_FileName = value;
}
}

#endregion

#region 導出一張表頁的Excel
/// <summary>
/// 導出帶有一張表頁的Excel
/// </summary>
/// <param name="theDgvExport">DgvExport類對象</param>
/// <param name="dgv">要導出的DataGrideView</param>
/// <param name="arrList">DataGrideView數據源(DataTable)</param>
/// <returns></returns>
public void DataExport(DgvExport theDgvExport,DataTable dt)
{
//-***************獲取excel對象***************
string saveFileName = "";
bool fileSaved = false;
//DataTable dt2 = (DataTable) dgv.DataSource;
//dt = dvtodt(dgv);

SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = theDgvExport.FileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;

if (saveFileName.IndexOf(":") < 0) //被點了取消
{
return;
}

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("無法啟動Excel,可能您的機器未安裝Excel", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

Microsoft.Office.Interop.Excel.Workbook workbook = xlApp.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;

// 列索引,行索引,總列數,總行數
int colIndex = 0;
int colCount = dt.Columns.Count; ;
int rowCount = dt.Rows.Count;
ArrayList alDataType = new ArrayList();

// *****************獲取數據*********************

//給Sheet命名
if (dt.TableName != null && dt.TableName != "")
{
worksheet.Name = dt.TableName;
}

for (int i = 0; i < dt.Columns.Count; i++)
{
string strName =dt.Columns[i].ColumnName;
alDataType.Add(dt.Rows[0]["" + strName + ""].GetType());

}

// 創建緩存數據
object[,] objData = new object[rowCount + 1, colCount];

// 獲取列標題
colIndex = 1;
for (int i = 0; i < colCount; i++)
{
//if (!dt.Columns[i].)
//{
// continue;
//}
objData[0, colIndex - 1] = dt.Columns[i].ColumnName.ToString();
colIndex++;
}

// 獲取具體數據、重置列號
for (int i = 0; i < rowCount; i++)
{
colIndex = 1;
for (int j = 0; j < colCount; j++)
{
objData[i + 1, colIndex - 1] = dt.Rows[i][j];
colIndex++;
}
}
colIndex = 0;
//*********************特殊數字格式*******************
for (int i = 0; i < alDataType.Count; i++)
{
Type ty = (Type)alDataType[i];
switch (ty.Name)
{
case "DBNull":
range = worksheet.get_Range(xlApp.Cells[2, i + 1], xlApp.Cells[rowCount + 1, i + 1]);
range.NumberFormat = "@";
break;
case "String":
range = worksheet.get_Range(xlApp.Cells[2, i + 1], xlApp.Cells[rowCount + 1, i + 1]);
range.NumberFormat = "@";
break;
case "DateTime":
range = worksheet.get_Range(xlApp.Cells[2, i + 1], xlApp.Cells[rowCount + 1, i + 1]);
range.NumberFormat = "yyyy-MM-dd";
break;
case "Decimal":
range = worksheet.get_Range(xlApp.Cells[2, i + 1], xlApp.Cells[rowCount + 1, i + 1]);
range.NumberFormat = "#,##0.00";
break;
case "Float":
range = worksheet.get_Range(xlApp.Cells[2, i + 1], xlApp.Cells[rowCount + 1, i + 1]);
range.NumberFormat = "#,##0.00";
break;
case "Money":
range = worksheet.get_Range(xlApp.Cells[2, i + 1], xlApp.Cells[rowCount + 1, i + 1]);
range.NumberFormat = "#,##0.00";
break;
case "Double":
range = worksheet.get_Range(xlApp.Cells[2, i + 1], xlApp.Cells[rowCount + 1, i + 1]);
range.NumberFormat = "#,##0.00";
break;
}
}

//********************* 寫入Excel*******************

range = worksheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[rowCount+1, colCount]);
range.Value2 = objData;

Application.DoEvents();

//*******************設置輸出格式******************************
//表頭
range = worksheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, colCount]);
range.Font.Size = 9;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

//表體
range = worksheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowCount + 1, colCount]);
range.Font.Size = 9;

//***************************保存**********************

if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}

catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("導出文件時出錯,文件可能正被打開!\n" + ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
fileSaved = false;
}

xlApp.Quit();
GC.Collect();//強行銷毀

IntPtr ff = new IntPtr(xlApp.Hwnd);
int k = 0;
GetWindowThreadProcessId(ff, out k);
System.Diagnostics.Process pss = System.Diagnostics.Process.GetProcessById(k);
pss.Kill();

if (fileSaved)
{
MessageBox.Show("導出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("導出失敗!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion

#region 導出帶有多張表頁的Excel
/// <summary>
/// 導出帶有多張表頁的Excel
/// </summary>
/// <param name="theDgvExport">DgvExport類對象</param>
/// <param name="dgv">要導出的DataGrideView</param>
/// <param name="arrList">DataGrideView數據源(DataTable)的集合</param>
/// <returns></returns>
public bool DataExports(DgvExport theDgvExport, DataGridView dgv, ArrayList arrList)
{
string saveFileName = "";
bool fileSaved = false;

return fileSaved;
}
#endregion

#region dataGridView轉換成dataTable
public DataTable dvtodt(DataGridView dv)
{
dt.Columns.Clear();
dt.Rows.Clear();
dt.Clear();
DataColumn dc;
for (int i = 0; i < dv.Columns.Count; i++)
{
dc = new DataColumn();
dc.ColumnName = dv.Columns[i].Name;
dt.Columns.Add(dc);
}
for (int j = 0; j < dv.Rows.Count; j++)
{
DataRow dr = dt.NewRow();
for (int x = 0; x < dv.Columns.Count; x++)
{
dr[x] = dv.Rows[j].Cells[x].Value;
}
dt.Rows.Add(dr);
}
return dt;
}

#endregion

}
}

㈧ 在線等~~~c#窗體DataGridView導出word出現問題 異常:數字必須介於 1 和 63 之間

document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);是不是SaveAs中的某個的參數有問題

㈨ 跪求c#代碼將datagridview表格中內容輸出到word中

public void ExportDataGridView(DataGridView srcDgv)
{
SaveFileDialog sfile = new SaveFileDialog();
sfile.AddExtension = true;
sfile.DefaultExt = ".doc";
sfile.Filter = "(*.doc)|*.doc";
if (sfile.ShowDialog() == DialogResult.OK)
{
object path = sfile.FileName;
Object none = System.Reflection.Missing.Value;
Word.Application wordApp = new Word.Application();
Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
//建立表格
Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count , srcDgv.Columns.Count, ref none, ref none);
try
{
for (int i = 0; i < srcDgv.Columns.Count; i++)//設置標題
{
table.Cell(0, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
}
for (int i = 1; i < srcDgv.Rows.Count; i++)//填充數據
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{
table.Cell(i+1, j + 1).Range.Text = srcDgv[j, i - 1].Value.ToString();
}
}
document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
document.Close(ref none, ref none, ref none);
MessageBox.Show("導出成功!");
}
finally
{
wordApp.Quit(ref none, ref none, ref none);
}
}
}

㈩ c# 怎麼把dataGridView的數據 用Excel導出! 詳細點啊

具體實例如下:

1.先去導入dll庫:Microsoft.Office.Interop.Excel.dl

閱讀全文

與datagridview導出word相關的資料

熱點內容
java字元串怎麼初始化為空格 瀏覽:202
男孩和繼母韓國電影 瀏覽:745
大數據和數學專業選哪個簡單 瀏覽:506
女裝大佬拍的倫理片有什麼 瀏覽:774
動漫電影主人公小男孩 瀏覽:201
宮鬥文女主有心機有手段 瀏覽:422
大數據中學生 瀏覽:259
微信公眾平台開放平台區別 瀏覽:679
微信發送mp4視頻 瀏覽:953
小電影網址求推薦 瀏覽:768
win10電腦圖標點不開怎麼辦 瀏覽:704
蘋果7的網路信號不好怎麼辦 瀏覽:223
男同激情電影 瀏覽:62
國外的電影去哪裡下載 瀏覽:991
送快遞電影 瀏覽:301
聯相電腦win7換win10 瀏覽:846
玄幻肉 瀏覽:991
pwd4.ocm 瀏覽:915
如何在linux安裝tomcat 瀏覽:974
哪個網校的編程最好 瀏覽:717

友情鏈接