private void btnConvert_Click(object sender, System.EventArgs e) { //Make sure something is selected. if (cmbBoxOutput.SelectedIndex == -1) { cmbBoxOutput.SelectedIndex = 0; } //Convert the input file to the save as format selected ConvertFile(); //We are done. MessageBox.Show("Convertsion Complete."); } private void ConvertFile() { String inFileName = txtBoxInputFile.Text; if (!File.Exists(inFileName)) { //Valid file wasn't entered. MessageBox.Show(inFileName+" does not exist. Please select an existing file to convert."); return; } //Get the myItem object from the selected item in the save as combo box. myItem tmpItem = cmbBoxOutput.SelectedItem as myItem; //Set some vars object fileName = inFileName; object fileSaveName = inFileName.Substring(0,inFileName.LastIndexOf(".")) + tmpItem.ItemExtension; //".txt"; object vk_read_only = false; object vk_visible = true; object vk_true = true; object vk_false = false; object vk_dynamic = 2; object missing = System.Reflection.Missing.Value; // the way to handle parameters you don't care about in .NET object vk_range = missing; object vk_to = missing; object vk_from = missing; Microsoft.Office.Interop.Word.ApplicationClass vk_word_app = new Microsoft.Office.Interop.Word.ApplicationClass();
// Open the document that was chosen by the dialog Microsoft.Office.Interop.Word.Document aDoc = null; try { aDoc = vk_word_app.Documents.Open( ref fileName, ref missing, ref vk_read_only, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref vk_visible, ref missing, ref missing, ref missing, ref missing ); } catch (System.Exception ex) {
MessageBox.Show("There was a problem opening "+fileName+" error:"+ex.ToString()); } // Save the doc as the format requested file try { //Get the word saveas format from the myItem object we got from the save as combo box selected item object vk_saveformat = tmpItem.ItemWord;
先来个简单的
word html
回车(\r\n) <p></p>
空格(" ")
1.伺副器端装WORD.
2.用WORD VBA的COM接口转换文档,输出格式用HTML.
3.用ASP.NET调用2.
4.将HTML输出.
{
//Make sure something is selected.
if (cmbBoxOutput.SelectedIndex == -1)
{ cmbBoxOutput.SelectedIndex = 0; } //Convert the input file to the save as format selected
ConvertFile(); //We are done.
MessageBox.Show("Convertsion Complete.");
}
private void ConvertFile()
{
String inFileName = txtBoxInputFile.Text;
if (!File.Exists(inFileName))
{ //Valid file wasn't entered.
MessageBox.Show(inFileName+" does not exist. Please select an existing file to convert.");
return;
}
//Get the myItem object from the selected item in the save as combo box.
myItem tmpItem = cmbBoxOutput.SelectedItem as myItem; //Set some vars
object fileName = inFileName;
object fileSaveName = inFileName.Substring(0,inFileName.LastIndexOf(".")) + tmpItem.ItemExtension; //".txt";
object vk_read_only = false;
object vk_visible = true;
object vk_true = true;
object vk_false = false;
object vk_dynamic = 2; object missing = System.Reflection.Missing.Value;
// the way to handle parameters you don't care about in .NET
object vk_range = missing;
object vk_to = missing;
object vk_from = missing;
Microsoft.Office.Interop.Word.ApplicationClass vk_word_app = new Microsoft.Office.Interop.Word.ApplicationClass();
// Open the document that was chosen by the dialog
Microsoft.Office.Interop.Word.Document aDoc = null;
try
{
aDoc = vk_word_app.Documents.Open(
ref fileName, ref missing, ref vk_read_only,
ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref vk_visible, ref missing, ref missing, ref missing, ref missing );
}
catch (System.Exception ex)
{
MessageBox.Show("There was a problem opening "+fileName+" error:"+ex.ToString());
}
// Save the doc as the format requested file
try
{ //Get the word saveas format from the myItem object we got from the save as combo box selected item
object vk_saveformat = tmpItem.ItemWord;
aDoc.SaveAs(ref fileSaveName, ref vk_saveformat ,ref missing,ref missing,ref missing, ref missing,
ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing,
ref missing, ref missing, ref missing);
}
catch (System.Exception ex)
{ MessageBox.Show("Error : "+ex.ToString());}
finally
{ //Don't forget to close everything up...
if (aDoc != null)
{
aDoc.Close(ref vk_false, ref missing, ref missing);
}
vk_word_app.Quit(ref vk_false,ref missing,ref missing);
}
}
用Microsoft.Office.Interop.Word.dll
望指教!
Type wordType = word.GetType();
Word.Documents docs = word.Documents; // 打开文件
Type docsType = docs.GetType();
object fileName = @"C:\姜治平.doc";
Word.Document doc = (Word.Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {fileName, true, true}); // 转换格式,另存为
Type docType = doc.GetType();
object saveFileName = @"C:\姜治平.htm";
//下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
//docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null,
// doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
///其它格式:
///wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null,
doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatHTML}); // 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);