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

一、使用C操作Word表格
要使用C操作Word表格,我們首先需要安裝一個Word的開發(fā)包。這里推薦使用Microsoft Office Interop Word Library。安裝完之后,我們就可以在C中利用COM(Component Object Model,組件對象模型)來操作Word。
具體來說,我們需要做以下幾個操作:
1. 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
使用下列代碼即可創(chuàng)建一個Word應(yīng)用程序?qū)ο螅?/p>
“`c
#include
#include
// 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
void create_word_app_obj() {
HRESULT hr;
// 初始化COM庫
CoInitialize(NULL);
// 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
IApplicationPtr iapp(“Word.Application”);
// 設(shè)置屬性
iapp->PutVisible(TRUE);
// 得到Documents對象
IDispatchPtr idisp = iapp->GetDocuments();
IDispatchPtr idoc;
// 新建一個Document
hr = idisp->CallMethod(L”Add”, &idoc);
}
“`
2. 創(chuàng)建表格
使用下列代碼即可創(chuàng)建一個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對象
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對象
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ù)庫
將數(shù)據(jù)存儲到數(shù)據(jù)庫中可以使用ODBC (Open DataBase Connectivity)技術(shù)。ODBC是一種支持異構(gòu)數(shù)據(jù)庫間通信的API接口標準。使用的過程中,需要先安裝ODBC數(shù)據(jù)庫驅(qū)動程序。
具體來說,我們需要做以下幾個操作:
1. 連接數(shù)據(jù)庫
使用下列代碼即可連接指定的數(shù)據(jù)庫:
“`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ù)庫
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ù)庫句柄
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
// 連接數(shù)據(jù)庫
retcode = SQLConnect(hdbc, dsn, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
}
“`
2. 插入數(shù)據(jù)
使用下列代碼即可將數(shù)據(jù)插入到指定的數(shù)據(jù)庫表中:
“`c
// 插入數(shù)據(jù)
void insert_data() {
// 創(chuàng)建并執(zhí)行SQL語句
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)容整合起來,就可以得到一個完整的C程序,將數(shù)據(jù)填充入Word表格并存入數(shù)據(jù)庫:
“`c
#include
#include
#include
#include
#include
// 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
void create_word_app_obj() {
HRESULT hr;
// 初始化COM庫
CoInitialize(NULL);
// 創(chuàng)建Word應(yīng)用程序?qū)ο?/p>
IApplicationPtr iapp(“Word.Application”);
// 設(shè)置屬性
iapp->PutVisible(TRUE);
// 得到Documents對象
IDispatchPtr idisp = iapp->GetDocuments();
IDispatchPtr idoc;
// 新建一個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對象
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對象
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ù)庫
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ù)庫句柄
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
// 連接數(shù)據(jù)庫
retcode = SQLConnect(hdbc, dsn, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
}
// 插入數(shù)據(jù)
void insert_data() {
// 創(chuàng)建并執(zhí)行SQL語句
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)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù)!
如何向Word中寫入數(shù)據(jù),C#.net
您好。這個問題我以前曾經(jīng)研究過,寫一篇博文來講述這個問題。
請參考我的博客,相信里面的內(nèi)容應(yīng)該是你要的。
《C#:簡單實現(xiàn)動態(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(); //新建一個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;
//添加二個命名控件
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);
//添加頁眉
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è)置右對齊
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出頁眉設(shè)置
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//設(shè)置文檔的行間距
//移動焦點并換行
object count = 14;
object WdLine = Word.WdUnits.wdLine;//換一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移動焦點
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)品詳細信息表”;
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)建時間:” + 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 = “文件導出異常!”;
}
return message;
}
}
web編程中怎樣把word中的表格導入到數(shù)據(jù)庫
建立新文件夾 然后把做好的 拉進去
我知道,閉埋
你先把ACCESS中的數(shù)據(jù)導出到EXCEL中,然后刪除EXCEL中的數(shù)據(jù),按照你的需要,把WORD表格中的數(shù)據(jù)復制到EXCEL中,然后把EXCEL中的數(shù)據(jù)導入到ACCESS數(shù)據(jù)庫中鬧鋒。
PS:
因為數(shù)據(jù)庫只支持EXCEL的導入和導出,所以要把WORD中的數(shù)轎彎螞據(jù)復制到EXCEL中。
這個要用到數(shù)據(jù)庫,用OFFICE里的那個ASSECC軟件
你先把ACCESS中的數(shù)據(jù)導出到EXCEL中,復制,找一個信箱,粘貼在里邊,發(fā)送給自己??纯葱Ч?/p>
用office中得vba操作word行嗎?
c word表格 填充數(shù)據(jù)庫的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于c word表格 填充數(shù)據(jù)庫,使用C編寫程序,將數(shù)據(jù)填充入Word表格并存入數(shù)據(jù)庫,如何向Word中寫入數(shù)據(jù),C#.net,web編程中怎樣把word中的表格導入到數(shù)據(jù)庫的信息別忘了在本站進行查找喔。
香港服務(wù)器選創(chuàng)新互聯(lián),香港虛擬主機被稱為香港虛擬空間/香港網(wǎng)站空間,或者簡稱香港主機/香港空間。香港虛擬主機特點是免備案空間開通就用, 創(chuàng)新互聯(lián)香港主機精選cn2+bgp線路訪問快、穩(wěn)定!
本文標題:使用C編寫程序,將數(shù)據(jù)填充入Word表格并存入數(shù)據(jù)庫(cword表格填充數(shù)據(jù)庫)
文章轉(zhuǎn)載:http://www.5511xx.com/article/dhigshg.html


咨詢
建站咨詢
