java openxml word_【转】【OpenXml】OpenXml操作Word的一些操作总结

article/2025/10/25 4:06:06

OpenXml相对于用MS提供的COM组件来生成WORD,有如下优势:

1.相对于MS 的COM组件,因为版本带来的不兼容问题,及各种会生成WORD半途会崩溃的问题.

2.对比填满一张30多页的WORD来说(包含图,表等),用COM组件来生成会占用20秒,Openxml1秒.

3.MS Word软件太贵了,你的客户装的是开源WORD,如LibreOffice,OpenOffice.这样你就只能用Openxml生成的WORD文档,各种支持MS Word都能打开,避免客户机器上安装MS Word.

简单来说OpenXml的各个操作.

首先用OpenXml打开一张报表.

public void CreateOpenXMLFile(stringfilePath)

{using (WordprocessingDocument objWordDocument =WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))

{

MainDocumentPart objMainDocumentPart=objWordDocument.AddMainDocumentPart();

objMainDocumentPart.Document= new Document(newBody());

Body objBody=objMainDocumentPart.Document.Body;//创建一些需要用到的样式,如标题3,标题4,在OpenXml里面,这些样式都要自己来创建的//ReportExport.CreateParagraphStyle(objWordDocument);

SectionProperties sectionProperties = newSectionProperties();

PageSize pageSize= newPageSize();

PageMargin pageMargin= newPageMargin();

Columns columns= new Columns() { Space = "220" };//720

DocGrid docGrid = new DocGrid() { LinePitch = 100 };//360//创建页面的大小,页距,页面方向一些基本的设置,如A4,B4,Letter,//GetPageSetting(PageSize,PageMargin);//在这里填充各个Paragraph,与Table,页面上第一级元素就是段落,表格.

objBody.Append(newParagraph());

objBody.Append(newTable());

objBody.Append(newParagraph());//我会告诉你这里的顺序很重要吗?下面才是把上面那些设置放到Word里去.(大家可以试试把这下面的代码放上面,会不会出现打开openxml文件有误,因为内容有误)

sectionProperties.Append(pageSize, pageMargin, columns, docGrid);

objBody.Append(sectionProperties);//如果有页眉,在这里添加页眉.

if(IsAddHead)

{//添加页面,如果有图片,这个图片和上面添加在objBody方式有点不一样,这里搞了好久.//ReportExport.AddHeader(objMainDocumentPart, image);

}

objMainDocumentPart.Document.Save();

}

}

发现上面有点注解说错,那个顺序不影响Word,但是影响如LibreOffice软件打开后看到的格式不正确.改里面太麻烦,直接在这说了.

从这个总纲里,把上面的各个步骤再来仔细说下.

首先是在Openxml创建标题3,标题4.

// 为文档创建段落样式

public static voidCreateParagraphStyle(WordprocessingDocument doc)

{//进入文档控制样式部分

StyleDefinitionsPart styleDefinitionsPart;

styleDefinitionsPart= doc.MainDocumentPart.AddNewPart();

Styles root= newStyles();

root.Save(styleDefinitionsPart);

Styles styles=styleDefinitionsPart.Styles;if (styles == null)

{

styleDefinitionsPart.Styles= newStyles();

styleDefinitionsPart.Styles.Save();

}

Style style3= CreateTitleStyle(3);

Style style4= CreateTitleStyle(4);//把样式添加入文档中

styles.Append(style3);

styles.Append(style4);

}private static Style CreateTitleStyle(inttitleIndex)

{string titleID =titleIndex.ToString();string rsid = string.Empty;string before = string.Empty;string after = string.Empty;string line = string.Empty;string val = string.Empty;int outline = titleIndex - 1;if (titleIndex == 3)

{

rsid= "00BA1E98";

before= "130";//"260"

after = "0";

line= "286";//"416"

val = "32";

}else if (titleIndex == 4)

{

rsid= "00BA1E98";

before= "88";

after= "0";

line= "288";//"376"

val = "28";

}

Style style2= new Style() { Type = StyleValues.Paragraph, StyleId =titleID };

StyleName styleName2= new StyleName() { Val = "heading" +titleID };

BasedOn basedOn1= new BasedOn() { Val = "a"};

NextParagraphStyle nextParagraphStyle1= new NextParagraphStyle() { Val = "a"};

LinkedStyle linkedStyle1= new LinkedStyle() { Val = titleID + "Char"};

UIPriority uIPriority1= new UIPriority() { Val = 9};

PrimaryStyle primaryStyle2= newPrimaryStyle();

Rsid rsid2= new Rsid() { Val =rsid };

style2.Append(styleName2);

style2.Append(basedOn1);

style2.Append(nextParagraphStyle1);

style2.Append(linkedStyle1);

style2.Append(uIPriority1);

style2.Append(primaryStyle2);

style2.Append(rsid2);

StyleParagraphProperties styleParagraphProperties2= newStyleParagraphProperties();

KeepNext keepNext1= newKeepNext();

KeepLines keepLines1= newKeepLines();

SpacingBetweenLines spacingBetweenLines1= new SpacingBetweenLines() { Before = before, After = after, Line = line, LineRule =LineSpacingRuleValues.Auto };

OutlineLevel outlineLevel1= new OutlineLevel() { Val =outline };

styleParagraphProperties2.Append(keepNext1);

styleParagraphProperties2.Append(keepLines1);

styleParagraphProperties2.Append(spacingBetweenLines1);

styleParagraphProperties2.Append(outlineLevel1);

style2.Append(styleParagraphProperties2);

StyleRunProperties styleRunProperties1= newStyleRunProperties();

Bold bold1= newBold();

BoldComplexScript boldComplexScript1= newBoldComplexScript();//Kern kern2 = new Kern() { Val = (UInt32)44U };

FontSize fontSize2 = new FontSize() { Val =val };

FontSizeComplexScript fontSizeComplexScript2= new FontSizeComplexScript() { Val =val };

styleRunProperties1.Append(bold1);

styleRunProperties1.Append(boldComplexScript1);//styleRunProperties1.Append(kern2);

styleRunProperties1.Append(fontSize2);

styleRunProperties1.Append(fontSizeComplexScript2);

style2.Append(styleRunProperties1);returnstyle2;

}

然后是对页面大小,页面方向,页距设置.(懒的和报表里的一个个对应,里面的页距有很多大家可以精确设置.)

public static void GetPageSetting(ref PageSize pageSize, refPageMargin pageMargin)

{bool val =IsPaperOrientation;string str_paperSize = "Letter";//A4,B4

UInt32Value width = 15840U;

UInt32Value height= 12240U;int top = 1440;

UInt32Value left= 1440U;if (str_paperSize == "A4")

{

width= 16840U;

height= 11905U;

}else if (str_paperSize == "B4")

{

width= 20636U;

height= 14570U;

}if (!val)

{

UInt32Value sweep=width;

width=height;

height=sweep;int top_sweep =top;

top= (int)left.Value;

left= (uint)top_sweep;

}

pageSize.Width=width;

pageSize.Height=height;

pageSize.Orient= new EnumValue(val ?PageOrientationValues.Landscape : PageOrientationValues.Portrait);

pageMargin.Top=top;

pageMargin.Bottom=top;

pageMargin.Left=left;

pageMargin.Right=left;

pageMargin.Header= (UInt32Value)720U;

pageMargin.Footer= (UInt32Value)720U;

pageMargin.Gutter= (UInt32Value)0U;

}

然后重点来了,大家对各元素如何在OpenXml添加的.

我先说下,在Openxml里相关元素的关系,在Word里,按一下Enter,转一行,对应的一行元素就是Paragraph,那如果一行文字在里面如何存放.Paragraph->Run->Text,图表Paragraph->Run->Drawing,表格Table->TableRow->TableCell->Paragraph->Run->Text与Drawing.在关系上说,还是很简洁的.来看一下具体的操作,我们先定义一个类.对应OpenXML里的基本样式设置.

public classReportCommon

{public const float defaultSize =12f;public const float H1 =20f;public const float H3 =16f;public const float H4 =14f;private string text = string.Empty;publicReportCommon()

{

Alignment= -1;

Size=defaultSize;

}//规定-1左对齐,0中间,1右对齐

public int Alignment { get; set; }public virtual float Size { get; set; }public string Text { get; set; }public virtual bool IsBold { get; set; }

}

其中我定义这个类的三个子类,分别是ReportValue:主要是这种A: B,ReportImage:包含一个图片的路径.ReportText:只有一个文本.

private static ListGetRuns(ReportCommon common)

{

List runs = new List();if (common isReportValue)

{

ReportValue reportvalue= common asReportValue;

Run r= newRun();

RunProperties rP= GetRunProperties(reportvalue, true);

r.Append(rP);string text =reportvalue.Text;if (text.EndsWith(":"))

text= text + " ";if (!text.EndsWith(":"))

text= text + ":";

Text t=CreateText(text);

r.Append(t);

runs.Add(r);

r= newRun();

rP= GetRunProperties(reportvalue, false);

r.Append(rP);

r.Append(CreateText(reportvalue.Value));

runs.Add(r);

}else if (common isReportImage)

{

ReportImage reportImage= common asReportImage;

Run r= newRun();

RunProperties rP=GetRunProperties(reportImage);

Drawing image= GetImageToBody(reportImage.RId, reportImage.Width * 600, reportImage.Height * 800);//Drawing image = new Drawing();//image.Append(new A.Blip() { Embed = new StringValue(reportImage.RId) });

r.Append(rP);

r.Append(image);

runs.Add(r);

}else if (common isReportText)

{

Run r= newRun();

RunProperties rP=GetRunProperties(common);

r.Append(rP);

r.Append(CreateText(common.Text));

runs.Add(r);

}returnruns;

}

看了这里,问题是不是越来越多.图片具体操作先不说.上面的RunProperties,与CreateText分别是指什么.

RunProperties是指包含这段Text,你要设置的一些字体大小,颜色,文本对齐设置.

而CreateText是因为我们在Openxml你在后面加个空格,而会给你过滤掉,空格要对应到XML的具体设置,看如下代码.

public static RunProperties GetRunProperties(ReportCommon common, bool bBold = false)

{

RunProperties rPr= newRunProperties();//Color color = new Color() { Val = "FF0000" };//the color is red

RunFonts rFont = newRunFonts();

rFont.Ascii= "Arial"; //the font is Arial//rPr.Append(color);//rPr.Append(rFont);

if (common.IsBold ||bBold)

rPr.Append(new Bold()); //it is Bold//TextAlignment

rPr.Append(new FontSize() { Val = new StringValue((common.Size * 2).ToString()) }); //font size (in 1/72 of an inch)

returnrPr;

}private static Text CreateText(stringtext)

{if (text == null)

text= string.Empty;

Text t= newText(text);if (text.EndsWith(" "))

{

t.Space= new EnumValue(SpaceProcessingModeValues.Preserve);

}if (text.StartsWith(" "))

{

t.Space= new EnumValue(SpaceProcessingModeValues.Default);

}returnt;

}

不知这么多代码大家看烦没,因为我这人喜欢不是业务与大纲的事,都喜欢直接看代码来说,比人讲的清楚.所以讲的时候也喜欢,直接上代码.废话不说了,说下图片的问题,Openxml插入图片比较麻烦,先贴一段代码.

private static Drawing GetImageToBody(string relationshipId, int x = 914400, int y = 360000)

{//Define the reference of the image.

var element =

newDrawing(newDW.Inline(new DW.Extent() { Cx = x, Cy =y },newDW.EffectExtent()

{

LeftEdge= 0L,

TopEdge= 0L,

RightEdge= 0L,

BottomEdge= 0L},newDW.DocProperties()

{

Id= (UInt32Value)1U,

Name= "Picture 1"},newDW.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks() { NoChangeAspect = true}),newA.Graphic(newA.GraphicData(newPIC.Picture(newPIC.NonVisualPictureProperties(newPIC.NonVisualDrawingProperties()

{

Id= (UInt32Value)0U,

Name= "New Bitmap Image.jpg"},newPIC.NonVisualPictureDrawingProperties()),newPIC.BlipFill(newA.Blip(newA.BlipExtensionList(newA.BlipExtension()

{

Uri=

"{28A0092B-C50C-407E-A947-70E740481C1C}"})

)

{

Embed=relationshipId,

CompressionState=A.BlipCompressionValues.Print

},newA.Stretch(newA.FillRectangle())),newPIC.ShapeProperties(newA.Transform2D(new A.Offset() { X = 0L, Y = 0L},new A.Extents() { Cx = x, Cy =y }),newA.PresetGeometry(newA.AdjustValueList()

) { Preset=A.ShapeTypeValues.Rectangle }))

) { Uri= "http://schemas.openxmlformats.org/drawingml/2006/picture"})

)

{

DistanceFromTop= (UInt32Value)0U,

DistanceFromBottom= (UInt32Value)0U,

DistanceFromLeft= (UInt32Value)0U,

DistanceFromRight= (UInt32Value)0U,

EditId= "50D07946"});//Append the reference to body, the element should be in a Run.

var blip = element.Descendants()

.FirstOrDefault();returnelement;

}

这段代码里,东东比较多,大家主要看这一节, Embed = relationshipId,也是参数里要求传入的,我们可以这么理解,在OpenXML插入一张电脑的图片,插入数据到word后,word然后把保存这个图片的一个标识量给我们,让我们来用,就是relationshipId.说到这,我们好像还没看如何把一张路径下的图片插入word.如下

public static voidCreateImageRid(ReportImage reportImage, MainDocumentPart objMainDocumentPart)

{

ImagePartType imagetype=ImagePartType.Jpeg;

FileInfo newImg= newFileInfo(reportImage.Value);

ImagePart newImgPart=objMainDocumentPart.AddImagePart(imagetype);//插入图片数据到Word里去.

using (FileStream stream =newImg.OpenRead())

{

newImgPart.FeedData(stream);

}//Word返回给我们插入数据的标识符.

reportImage.RId =objMainDocumentPart.GetIdOfPart(newImgPart);

}

这里图片插入有先后关系,要先调用上面这段,插入数据到word,然后才能调用上上段的那段代码来生成Drawing元素.

大家如果要把图片的宽度,设为当前Word的可用宽度.Int32Value width = (int)(pageSize.Width - pageMargin.Right - pageMargin.Left);

好吧,大家会发现上上段那里的长度特大,这里发现这里的值要很大才能显现比较好看的图片,一般我在原来的基础宽度*600,基础长度*800.原因吗,我也不清楚,有些猜测,没有验证就不说了,这个要求OpenXML生成报表比较急,我把这几天所有操作先总结一下.后面再来修改,如果有知道的道友,不妨说一下.

在这里,文字与图片如何生成Paragraph就很简单了.

public staticParagraph GetParagraph(ReportCommon common)

{

Paragraph p= newParagraph();

ParagraphProperties pPr=GetParagraphProperties(common);

p.Append(pPr);

List run =GetRuns(common);foreach (var r inrun)

{

p.Append(r);

}returnp;

}

好吧,最后说到如何在OpenXML生成一张表.生成图表我用的基础数据是List>,上面的ReportTable数据就放在这个里面.当然还有一些基本的定义属性就不说.具体如下看代码.

public staticTable GetParagraph(ReportTable reportTable, Int32Value width)

{

Table table= newTable();

TableProperties tblProp= newTableProperties(newTableBorders(new TopBorder() { Val = new EnumValue(BorderValues.Single), Size = 4},new BottomBorder() { Val = new EnumValue(BorderValues.Single), Size = 4},new LeftBorder() { Val = new EnumValue(BorderValues.Single), Size = 4},new RightBorder() { Val = new EnumValue(BorderValues.Single), Size = 4},new InsideHorizontalBorder() { Val = new EnumValue(BorderValues.Single), Size = 4},new InsideVerticalBorder() { Val = new EnumValue(BorderValues.Single), Size = 4}

)

);

tblProp.TableWidth= new TableWidth() { Width = width.ToString(), Type =TableWidthUnitValues.Dxa };

table.Append(tblProp);int count =reportTable.Value.Count;int cols =reportTable.Column;int j = 0;foreach (List strs inreportTable.Value)

{

TableRow row= newTableRow();for (int i = 0; i < cols; i++)

{

TableCell cell= newTableCell();

TableCellProperties tableCellProperties= newTableCellProperties();

TableCellMargin margin= newTableCellMargin();

margin.LeftMargin= new LeftMargin() { Width = "100", Type =TableWidthUnitValues.Dxa };

margin.RightMargin= new RightMargin() { Width = "100", Type =TableWidthUnitValues.Dxa };

tableCellProperties.Append(margin);

Paragraph par= newParagraph();

Run run= newRun();if (j == 0 &&reportTable.IsHaveColumn)

{

RunProperties rPr= newRunProperties();

rPr.Append(newBold());

run.Append(rPr);

}if (strs.Count != cols && i >= strs.Count - 1)

{

HorizontalMerge verticalMerge= newHorizontalMerge();if (i == strs.Count - 1)

{

RunProperties rPr= newRunProperties();

rPr.Append(newBold());

run.Append(rPr);

verticalMerge.Val=MergedCellValues.Restart;

run.Append(CreateText(strs[i]));

}else{

verticalMerge.Val=MergedCellValues.Continue;

}

tableCellProperties.Append(verticalMerge);

}else{

run.Append(CreateText(strs[i]));

}

par.Append(run);

cell.Append(tableCellProperties);

cell.Append(par);

row.Append(cell);

}

j++;

table.Append(row);

}returntable;

}

代码可以简单的多,只是因为有一些要求,比如你表每行是5个数据,但是有一行,数据只有四个,有二个表格合并了,这里会默认把最后二格合并,具体意思大家可以改改代码看.

好吧,到这里,就差不多,元素添加完后,然后是把相关页面大小的设置加到objBody,最后是添加页眉.

public static voidAddHeader(MainDocumentPart mainDocPart, ReportImage reportImge)

{//Delete the existing header parts.

mainDocPart.DeleteParts(mainDocPart.HeaderParts);//Create a new header part and get its relationship id.

HeaderPart newHeaderPart = mainDocPart.AddNewPart();string rId =mainDocPart.GetIdOfPart(newHeaderPart);

ImagePart imagepart=newHeaderPart.AddImagePart(ImagePartType.Jpeg);

FileInfo newImg= newFileInfo(reportImge.Value);using (FileStream stream =newImg.OpenRead())

{

imagepart.FeedData(stream);

}string imageRID =newHeaderPart.GetIdOfPart(imagepart);

reportImge.RId=imageRID;

Header header=GeneratePageHeaderPart(reportImge);

header.Save(newHeaderPart);foreach (SectionProperties sectProperties inmainDocPart.Document.Descendants())

{//Delete any existing references to headers.

foreach (HeaderReference headerReference insectProperties.Descendants())

sectProperties.RemoveChild(headerReference);

HeaderReference newHeaderReference=

new HeaderReference() { Id = rId, Type =HeaderFooterValues.Default };

sectProperties.Append(newHeaderReference);

}

header.Save();

}//Creates an header instance and adds its children.

private staticHeader GeneratePageHeaderPart(ReportImage reportImge)

{var runs =GetRuns(reportImge);

Paragraph paragraph= newParagraph();

paragraph.Append(GetParagraphProperties(reportImge));foreach (var run inruns)

{

paragraph.Append(run);

}

paragraph.Append(new Run(new Text() { Text = ""}));

Header header= newHeader();

header.Append(paragraph);returnheader;

}

上面的代码主要注意,Image所指的路径存放的和在前面的Document里不一样,这里存放在Header里,从Word文件解压来说,二都是不同的XML文档,你把图片数据写在Document里,得到的标识符在Header是空的.

好了.OpenXML的主要操作都在这了,但是大家如果想生成这种样式,应该如何处理?

945dbff683c351b3eca7e24b6bf8ec74.png

上面每一个元素如Payload Mass (LBS): 0.22046,就是前面的ReportValue,如何以这种对齐方式来插入了?引入一个新的结构,ReportValueList,主要就是ReportValue的键表信息.

public static List GetParagraph(ReportValueList valueList, Int32Value width, int column = 2)

{if (column < 1)

column= 1;

List list = new List();int currentcolumn = 0;

Paragraph currentParagraph= null;foreach (var reportvalue invalueList.Values)

{

reportvalue.Size=valueList.Size;if (currentcolumn == 0)

{

currentParagraph= newParagraph();

ParagraphProperties pPr= newParagraphProperties();//添加标签类

Tabs tabs = newTabs();

Int32Value eachWidth= width / (newInt32Value(column));for (int i = 1; i < column; i++)

{

TabStop stop= newTabStop();

stop.Val= new EnumValue(TabStopValues.Left);

stop.Position= eachWidth *i;

tabs.Append(stop);

}

pPr.Append(tabs);

currentParagraph.Append(pPr);

list.Add(currentParagraph);

}

List runs =GetRuns(reportvalue);foreach (var run inruns)

{

currentParagraph.Append(run);

}

currentcolumn++;if (currentcolumn

{

Run run= newRun();

run.Append(newTabChar());

currentParagraph.Append(run);

}if (currentcolumn >=column)

{

currentcolumn= 0;

}

}returnlist;

}

主要是对元素TabStop的运用,仔细的大家可以去查查文档

写到这里,忘记写要用到的命名空间.

using A = DocumentFormat.OpenXml.Drawing;

using PIC = DocumentFormat.OpenXml.Drawing.Pictures;

using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;

这个就到这里了.下面如果有时间,我会讲一下,如果对整张报表来进行模版化,意思是在word里的每一个表,一个图片,一个文字,在软件上用生成的结果来替换相应的元素.


http://chatgpt.dhexx.cn/article/wyx2UlwD.shtml

相关文章

java openxml 操作 word_OpenXML操作word

OpenXML概述 项目中经常需要操作word,之前的方式是采用COM接口,这个接口很不稳定,经常报错。现在开始采用OpenXML。OpenXML(OOXML)是微软在Office 2007中提出的一种新的文档格式,Office 2007中的Word、Excel、PowerPoint默认均采用OpenXML格式 。 OpenXml相对于用MS提供的C…

openXMl

openXMl注意点 一、基础标签 w:pStyle 定义样式ID w:keepNext &#xff1a;段落是否在一个页面上 w:keepLines &#xff1a;此元素指定在页面视图中呈现该文档时&#xff0c;尽可能将该段落的所有行维护在单个页面上 w:widowControl &#xff1a;这个元素指定消费者是否应该通…

OpenXML库(office文档读写库)的安装

本体安装 OpenXml库是由微软维护的一个开源的Office文档读写库&#xff0c;其与其他类似用途的库的比较可以看到这篇文章。 在C#中使用OpenXml非常简单&#xff0c;只需要使用NuGet安装其程序包即可&#xff0c;流程如下(NuGet这东西真的是个神器啊&#xff01;)&#xff1a;…

java e.getmessage() null_Java e.getMessage 错误信息为null

解决方案&#xff1a;用e.printStackTrace() e.getMessage()为null也很正常&#xff0c;NullPointException的getMessage()就为null 我在抛出Exception时也可以直接 throw new MyException(); 这样getMessage()返回的就是null了 这几天做的项目&#xff0c;测试小组说测出来的…

java中getmessage函数_PeekMessage、GetMessage的区别

在Windows编程中经常使用这两个函数来处理消息&#xff0c;它们之间的区别就是GetMessage是阻塞的&#xff0c;PeekMessage是非阻塞的。 GetMessage原型如下&#xff1a;BOOL GetMessage(LPMSG lpMsg,HWND hWnd,UINT wMsgFilterMin,UINT wMsgFilterMax); PeekMessage原型如下&a…

java e.getmessage() null,Java 求助! 为什么我拿不到错误信息,e.getMessage()

Java 求助! 为什么我拿不到错误信息,e.getMessage() Java 求助! 为什么我拿不到错误信息,e.getMessage() 我明明打印了错误信息的啊? e.getMessage() 是空,为什么? 一声声带着惊讶,带着恐慌,带着质疑,带着无奈的话语,从某个角落里传出。 议论纷纷...... 似乎拿不到异…

Java 异常中 e.getMessage() 和 e.toString() e.printStackTrace()的区别常见的几种异常

Java 异常中 e.getMessage() 和 e.toString() e.printStackTrace()的区别 一、概述 在java异常体系中&#xff0c;要打印异常信息&#xff0c;可以通过&#xff1a;e.getMessage() 、 e.toString() e.printStackTrace() 等方法打印出 一些 异常信息。已知的是这些方法都可以打…

GetMessage以及消息循环说明

GetMessage以及消息循环说明 在创建窗口、显示窗口、更新窗口后&#xff0c;我们需要编写一个消息循环&#xff0c;不断地从消息队列中取出消息&#xff0c;并进行响应。要从消息队列中取出消息&#xff0c;我们需要调用GetMessage()函数&#xff0c;该函数的原型声明如下&…

redis使用配置文件的方式启动

文章目录 为什么用配置文件启动呢使用配置文件启动1.创建配置文件2.修改配置文件里面的参数3.以配置文件的方式启动4.查看日志文件 为什么用配置文件启动呢 因为在企业项目中不会只有单个redis实例&#xff0c;每个redis的config配置都可以能不一样&#xff0c;所以这个时候就需…

.NET Core使用Redis

环境Windows 首先安装Redis 1、下载最新版redis&#xff0c;选择.zip则是免安装的版本 下载地址&#xff1a;https://github.com/MicrosoftArchive/redis/releases 2、解压到指定目录&#xff0c;并运行cmd命令 3、在该文件夹下运行命令&#xff1a;redis-server.exe redis.…

flask使用redis

文章目录 前言一、环境二、使用步骤1.安装redis1.下载2.运行 2.安装并使用redis包1.安装2.使用 运行 前言 有人会有疑问&#xff0c;不是已经连上了mysql数据库了吗&#xff0c;为什么还要用redis数据库?有时候用户访问网页的时候&#xff0c;会产生一些临时性的数据&#xf…

最新版本Redis:Redis 7.0 安装使用,编译安装Redis,Redis使用场景-2022年最新Redis图解安装

最新版本Redis&#xff1a;Redis 7.0 安装使用&#xff0c;编译安装Redis&#xff0c;Redis使用场景。 本章主要目标&#xff1a; 1.Redis介绍 2.下载Redis&#xff0c;编译安装Redis 3.启动Redis使用 4.Redis常用数据类型 5.使用场景介绍 下面开始本章内容的学习 1.Redi…

TP5 使用redis

1.打开tp5官方手册&#xff0c;在缓存下面找到使用多个缓存类型 粘贴以下代码到tp框架config下面的缓存设置下 // 切换到redis操作 Cache::store(redis)->set(name,value);//获取redis Cache::store(redis)->get(rename); //实现redis 消息队列 在 /thinkphp/library/t…

C#使用Redis

Redis是一种key-value型数据库与非持久化数据,用来存于频繁操作的数据&#xff0c;与Sqlserver、Mysql等关系型数据库有着很大的区别&#xff0c;如Redis存储的数据结构不相同&#xff0c;还有Redis数据存于内存中&#xff0c;它也与Mongodb、Azure Cosmos Db、Azure TableStor…

springboot使用redis

springboot使用redis redis-service.exe : 服务端,启动后不要关闭 redis-cli.exe : 客户端,访问redis中的数据 redisclient-win32.x86_64.2.0.jar : redis的图形界面客户端,执行方式是在这个文件的目录执行 java -jar redisclient-win32.x86_64.2.0.jar或者在这个jar包的目录…

springboot集成redis使用注解

redis简介&#xff1a; Redis是当前比较热门的NOSQL系统之一&#xff0c;它是一个开源的使用ANSI c语言编写的key-value存储系统 &#xff08;区别于MySQL的二维表格的形式存储。&#xff09;。和Memcache类似&#xff0c;但很大程度补偿了Memcache的不 足。和Memcache一样&am…

Another Redis使用手册

Another Redis DeskTop Manager 跨平台、免费的redis可视化工具&#xff0c;完胜Redis DeskTop Manager。 下载地址&#xff1a;https://github.com/qishibo/AnotherRedisDesktopManager 界面、排版引起极度舒适&#xff0c;只是命令行的入口相对隐蔽&#xff0c;在此截图标出…

Redis使用认证密码登录

Redis默认配置是不需要密码认证的&#xff0c;也就是说只要连接的Redis服务器的host和port正确&#xff0c;就可以连接使用。这在安全性上会有一定的问题&#xff0c;所以需要启用Redis的认证密码&#xff0c;增加Redis服务器的安全性。 1. 修改配置文件 Redis的配置文件默认…

Redis使用密码登录

&#xff08;1&#xff09;进入redis客户端安装目录&#xff1a; &#xff08;2&#xff09;输入命令&#xff1a;redis-cli.exe -h 主机号 -p 端口号 如&#xff1a;redis-cli.exe -h 127.0.0.1-p 6379 &#xff08;3&#xff09;输入密码命令&#xff1a;auth 密码 若显示…

thinkphp5 redis使用

服务器必须要安装redis 我是通过宝塔安装的redis 不过也可以通过命令安装 测试安装成功 进入redis redis-cli 查看密码 config get requirepass 设置密码 config set requirepass “123456” 使用redis有两种方式 第一种是使用php里面的redis扩展&#xff08;需要对服务器上…