新聞中心
隨著計(jì)算機(jī)技術(shù)的不斷進(jìn)步,數(shù)據(jù)的處理和管理變得越來(lái)越重要。在許多工作中,我們需要將數(shù)據(jù)整理成表格的形式進(jìn)行處理和統(tǒng)計(jì),這就需要用到電子表格軟件。而對(duì)于一些特定的工作,需要將表格中的數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中,以便于后續(xù)的數(shù)據(jù)分析和使用。本文將介紹如何。

一、使用C操作Word表格
要使用C操作Word表格,我們首先需要安裝一個(gè)Word的開發(fā)包。這里推薦使用Microsoft Office Interop Word Library。安裝完之后,我們就可以在C中利用COM(Component Object Model,組件對(duì)象模型)來(lái)操作Word。
具體來(lái)說(shuō),我們需要做以下幾個(gè)操作:
1. 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
使用下列代碼即可創(chuàng)建一個(gè)Word應(yīng)用程序?qū)ο螅?/p>
“`c
#include
#include
// 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
void create_word_app_obj() {
HRESULT hr;
// 初始化COM庫(kù)
CoInitialize(NULL);
// 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
IApplicationPtr iapp(“Word.Application”);
// 設(shè)置屬性
iapp->PutVisible(TRUE);
// 得到Documents對(duì)象
IDispatchPtr idisp = iapp->GetDocuments();
IDispatchPtr idoc;
// 新建一個(gè)Document
hr = idisp->CallMethod(L”Add”, &idoc);
}
“`
2. 創(chuàng)建表格
使用下列代碼即可創(chuàng)建一個(gè)2行3列的Word表格:
“`c
// 創(chuàng)建表格
void create_table() {
HRESULT hr;
IApplicationPtr iapp(“Word.Application”);
IDispatchPtr idisp = iapp->GetDocuments();
IDispatchPtr idoc;
hr = idisp->CallMethod(L”Add”, &idoc);
// 得到Selection對(duì)象
ISelectionPtr isel(iapp->GetSelection());
// 插入表格
isel->CallMethod(L”TablesAdd”, NULL, 2, 3);
}
“`
3. 填充表格數(shù)據(jù)
使用下列代碼即可將數(shù)據(jù)填充入表格:
“`c
// 填充表格數(shù)據(jù)
void fill_data() {
HRESULT hr;
IApplicationPtr iapp(“Word.Application”);
IDispatchPtr idisp = iapp->GetDocuments();
IDispatchPtr idoc;
hr = idisp->CallMethod(L”Add”, &idoc);
// 得到Selection對(duì)象
ISelectionPtr isel(iapp->GetSelection());
// 插入表格
isel->CallMethod(L”TablesAdd”, NULL, 2, 3);
// 填充表格數(shù)據(jù)
IDispatchPtr itab = isel->GetTables()->Item(1);
IDispatchPtr icells = itab->GetRange();
int data[2][3] = { {1, 2, 3}, {4, 5, 6} };
for (int i = 0; i
for (int j = 0; j
IDispatchPtr icell = icells->CallMethod(L”Item”, &vtMissing, i + 1, j + 1);
icell->PutRangeText(_bstr_t(data[i][j]), vtMissing);
}
}
}
“`
二、將數(shù)據(jù)存入數(shù)據(jù)庫(kù)
將數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中可以使用ODBC (Open DataBase Connectivity)技術(shù)。ODBC是一種支持異構(gòu)數(shù)據(jù)庫(kù)間通信的API接口標(biāo)準(zhǔn)。使用的過(guò)程中,需要先安裝ODBC數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序。
具體來(lái)說(shuō),我們需要做以下幾個(gè)操作:
1. 連接數(shù)據(jù)庫(kù)
使用下列代碼即可連接指定的數(shù)據(jù)庫(kù):
“`c
#include
#include
#include
RETCODE retcode;
SQLHANDLE henv;
SQLHANDLE hdbc;
SQLHANDLE hstmt;
SQLWCHAR* dsn = L”test”;
SQLWCHAR* uid = L”root”;
SQLWCHAR* pwd = L”123456″;
// 連接數(shù)據(jù)庫(kù)
void connect() {
// 初始化環(huán)境句柄
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
// 初始化數(shù)據(jù)庫(kù)句柄
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
// 連接數(shù)據(jù)庫(kù)
retcode = SQLConnect(hdbc, dsn, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
}
“`
2. 插入數(shù)據(jù)
使用下列代碼即可將數(shù)據(jù)插入到指定的數(shù)據(jù)庫(kù)表中:
“`c
// 插入數(shù)據(jù)
void insert_data() {
// 創(chuàng)建并執(zhí)行SQL語(yǔ)句
SQLWCHAR* sql = L”INSERT INTO mytable (id, name, age) VALUES (?, ?, ?)”;
retcode = SQLPrepare(hstmt, sql, SQL_NTS);
int data[2][3] = { {1, 2, 3}, {4, 5, 6} };
for (int i = 0; i
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_INTEGER, 0, 0, &data[i][0], 0, NULL);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_VARCHAR, 0, 0, “Amy”, 0, NULL);
SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_INTEGER, 0, 0, &data[i][1], 0, NULL);
retcode = SQLExecute(hstmt);
}
}
“`
三、程序?qū)崿F(xiàn)
將以上內(nèi)容整合起來(lái),就可以得到一個(gè)完整的C程序,將數(shù)據(jù)填充入Word表格并存入數(shù)據(jù)庫(kù):
“`c
#include
#include
#include
#include
#include
// 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
void create_word_app_obj() {
HRESULT hr;
// 初始化COM庫(kù)
CoInitialize(NULL);
// 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
IApplicationPtr iapp(“Word.Application”);
// 設(shè)置屬性
iapp->PutVisible(TRUE);
// 得到Documents對(duì)象
IDispatchPtr idisp = iapp->GetDocuments();
IDispatchPtr idoc;
// 新建一個(gè)Document
hr = idisp->CallMethod(L”Add”, &idoc);
}
// 創(chuàng)建表格
void create_table() {
HRESULT hr;
IApplicationPtr iapp(“Word.Application”);
IDispatchPtr idisp = iapp->GetDocuments();
IDispatchPtr idoc;
hr = idisp->CallMethod(L”Add”, &idoc);
// 得到Selection對(duì)象
ISelectionPtr isel(iapp->GetSelection());
// 插入表格
isel->CallMethod(L”TablesAdd”, NULL, 2, 3);
}
// 填充表格數(shù)據(jù)
void fill_data() {
HRESULT hr;
IApplicationPtr iapp(“Word.Application”);
IDispatchPtr idisp = iapp->GetDocuments();
IDispatchPtr idoc;
hr = idisp->CallMethod(L”Add”, &idoc);
// 得到Selection對(duì)象
ISelectionPtr isel(iapp->GetSelection());
// 插入表格
isel->CallMethod(L”TablesAdd”, NULL, 2, 3);
// 填充表格數(shù)據(jù)
IDispatchPtr itab = isel->GetTables()->Item(1);
IDispatchPtr icells = itab->GetRange();
int data[2][3] = { {1, 2, 3}, {4, 5, 6} };
for (int i = 0; i
for (int j = 0; j
IDispatchPtr icell = icells->CallMethod(L”Item”, &vtMissing, i + 1, j + 1);
icell->PutRangeText(_bstr_t(data[i][j]), vtMissing);
}
}
}
// 連接數(shù)據(jù)庫(kù)
void connect_database() {
RETCODE retcode;
SQLHANDLE henv;
SQLHANDLE hdbc;
SQLHANDLE hstmt;
SQLWCHAR* dsn = L”test”;
SQLWCHAR* uid = L”root”;
SQLWCHAR* pwd = L”123456″;
// 初始化環(huán)境句柄
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
// 初始化數(shù)據(jù)庫(kù)句柄
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
// 連接數(shù)據(jù)庫(kù)
retcode = SQLConnect(hdbc, dsn, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
}
// 插入數(shù)據(jù)
void insert_data() {
// 創(chuàng)建并執(zhí)行SQL語(yǔ)句
SQLWCHAR* sql = L”INSERT INTO mytable (id, name, age) VALUES (?, ?, ?)”;
retcode = SQLPrepare(hstmt, sql, SQL_NTS);
int data[2][3] = { {1, 2, 3}, {4, 5, 6} };
for (int i = 0; i
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_INTEGER, 0, 0, &data[i][0], 0, NULL);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_VARCHAR, 0, 0, “Amy”, 0, NULL);
SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_INTEGER, 0, 0, &data[i][1], 0, NULL);
retcode = SQLExecute(hstmt);
}
}
// 主函數(shù)
int mn() {
create_word_app_obj();
create_table();
fill_data();
connect_database();
insert_data();
return 0;
}
“`
四、
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián)為您提供網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù)!
如何向Word中寫入數(shù)據(jù),C#.net
您好。這個(gè)問(wèn)題我以前曾經(jīng)研究過(guò),寫一篇博文來(lái)講述這個(gè)問(wèn)題。
請(qǐng)參考我的博客,相信里面的內(nèi)容應(yīng)該是你要的。
《C#:簡(jiǎn)單實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)生成Word文檔并保存》
謝謝采納。
利用如下代碼即可:
C# code
public bool ExportWord(DataSet ds, string saveFileName)
{
bool fileSaved = false;
object filename = saveFileName;
try
{
Object Nothing = System.Reflection.Missing.Value;
//創(chuàng)建Word文檔
Word.Application WordApp = new Word.ApplicationClass();
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//文檔中創(chuàng)建表格
WordApp.Selection.TypeParagraph();
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 13, 5, ref Nothing, ref Nothing);
//設(shè)置表格樣式
//newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
//newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns.Width = 100f;
newTable.Columns.Width = 100f;
newTable.Columns.Width = 100f;
newTable.Columns.Width = 50f;
newTable.Columns.Width = 50f;
//填充表格內(nèi)容
newTable.Cell(1, 1).Range.Text = “登記表”;
newTable.Cell(1, 1).Range.Bold = 3;//設(shè)置單元格中字體為粗體
//合并單元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 5));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
object missing = System.Reflection.Missing.Value;
object unit;
unit = Word.WdUnits.wdStory;
WordApp.Selection.EndKey(ref unit, ref missing);
WordApp.Selection.TypeParagraph();
//文件保存
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
fileSaved = true;
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
if (WordApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
WordApp = null;
}
GC.Collect()
}
catch
{
fileSaved = false;
}
return fileSaved;
}
private void but_Table_Click(object sender, EventArgs e)
{
object Nothing = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
//創(chuàng)建Word文檔
Word.Application wordApp = new Word.ApplicationClass();
Word.Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordApp.Visible = true;
//設(shè)置文檔寬度
wordApp.Selection.PageSetup.LeftMargin = wordApp.CentimetersToPoints(float.Parse(“2”));
wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled = 11;
wordApp.Selection.PageSetup.RightMargin = wordApp.CentimetersToPoints(float.Parse(“2”));
Object start = Type.Missing;
Object end = Type.Missing;
PictureBox pp = new PictureBox(); //新建一個(gè)PictureBox控件
int p1 = 0;
for (int i = 0; i COM->Microsoft Word 11.0 Object Library
2、在.cs文件中添加CreateWordFile()方法
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//添加二個(gè)命名控件
using Word;
using System.IO;
namespace WordFile
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateWordFile(“D:\\dd.doc”);//調(diào)用方法
}
public string CreateWordFile(string CheckedInfo)
{
string message = “”;
try
{
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory(“D:/CNSI”); //創(chuàng)建文件所在目錄
string name = “CNSI_” + DateTime.Now.ToLongDateString() + “.doc”;//文件名
object filename = ” + name; //文件保存路徑
//創(chuàng)建Word文檔
Word.Application WordApp = new Word.ApplicationClass();
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//添加頁(yè)眉
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(“”);
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//設(shè)置右對(duì)齊
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出頁(yè)眉設(shè)置
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//設(shè)置文檔的行間距
//移動(dòng)焦點(diǎn)并換行
object count = 14;
object WdLine = Word.WdUnits.wdLine;//換一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移動(dòng)焦點(diǎn)
WordApp.Selection.TypeParagraph();//插入段落
//文檔中創(chuàng)建表格
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
//設(shè)置表格樣式
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle =Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns.Width = 100f;
newTable.Columns.Width = 220f;
newTable.Columns.Width = 105f;
//填充表格內(nèi)容
newTable.Cell(1, 1).Range.Text = “產(chǎn)品詳細(xì)信息表”;
newTable.Cell(1, 1).Range.Bold = 2;//設(shè)置單元格中字體為粗體
//合并單元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
//填充表格內(nèi)容
newTable.Cell(2, 1).Range.Text = “產(chǎn)品基本信息”;
newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//設(shè)置單元格內(nèi)字體顏色
//合并單元格
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//填充表格內(nèi)容
newTable.Cell(3, 1).Range.Text = “品牌名稱:”;
newTable.Cell(3, 2).Range.Text = “BrandName”;
//縱向合并單元格
newTable.Cell(3, 3).Select();//選中一行
object moveUnit = Word.WdUnits.wdLine;
object moveCount = 5;
object moveExtend = Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
//插入圖片
string FileName = @”d:\8.jpg”;//圖片所在路徑
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
WordDoc.Application.ActiveDocument.InlineShapes.Width = 100f;//圖片寬度
WordDoc.Application.ActiveDocument.InlineShapes.Height = 100f;//圖片高度
//將圖片設(shè)置為四周環(huán)繞型
Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes.ConvertToShape();
s.WrapFormat.Type =Word.WdWrapType.wdWrapSquare;
newTable.Cell(12, 1).Range.Text = “產(chǎn)品特殊屬性”;
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
//在表格中增加行
WordDoc.Content.Tables.Rows.Add(ref Nothing);
WordDoc.Paragraphs.Last.Range.Text = “文檔創(chuàng)建時(shí)間:” + DateTime.Now.ToString();//“落款”
WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
//文件保存
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
message = name + “文檔生成成功,以保存到D:CNSI下”;
Response.Write(message);
}
catch
{
message = “文件導(dǎo)出異常!”;
}
return message;
}
}
web編程中怎樣把word中的表格導(dǎo)入到數(shù)據(jù)庫(kù)
建立新文件夾 然后把做好的 拉進(jìn)去
我知道,閉埋
你先把ACCESS中的數(shù)據(jù)導(dǎo)出到EXCEL中,然后刪除EXCEL中的數(shù)據(jù),按照你的需要,把WORD表格中的數(shù)據(jù)復(fù)制到EXCEL中,然后把EXCEL中的數(shù)據(jù)導(dǎo)入到ACCESS數(shù)據(jù)庫(kù)中鬧鋒。
PS:
因?yàn)閿?shù)據(jù)庫(kù)只支持EXCEL的導(dǎo)入和導(dǎo)出,所以要把WORD中的數(shù)轎彎螞據(jù)復(fù)制到EXCEL中。
這個(gè)要用到數(shù)據(jù)庫(kù),用OFFICE里的那個(gè)ASSECC軟件
你先把ACCESS中的數(shù)據(jù)導(dǎo)出到EXCEL中,復(fù)制,找一個(gè)信箱,粘貼在里邊,發(fā)送給自己??纯葱Ч?/p>
用office中得vba操作word行嗎?
c word表格 填充數(shù)據(jù)庫(kù)的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于c word表格 填充數(shù)據(jù)庫(kù),使用C編寫程序,將數(shù)據(jù)填充入Word表格并存入數(shù)據(jù)庫(kù),如何向Word中寫入數(shù)據(jù),C#.net,web編程中怎樣把word中的表格導(dǎo)入到數(shù)據(jù)庫(kù)的信息別忘了在本站進(jìn)行查找喔。
香港服務(wù)器選創(chuàng)新互聯(lián),香港虛擬主機(jī)被稱為香港虛擬空間/香港網(wǎng)站空間,或者簡(jiǎn)稱香港主機(jī)/香港空間。香港虛擬主機(jī)特點(diǎn)是免備案空間開通就用, 創(chuàng)新互聯(lián)香港主機(jī)精選cn2+bgp線路訪問(wèn)快、穩(wěn)定!
分享題目:使用C編寫程序,將數(shù)據(jù)填充入Word表格并存入數(shù)據(jù)庫(kù)(cword表格填充數(shù)據(jù)庫(kù))
瀏覽地址:http://fisionsoft.com.cn/article/dhigshg.html


咨詢
建站咨詢
