删除XML文件中的额外引号

时间:2015-05-14 04:57:29

标签: xml vba excel-vba excel

我正在制作一个快速而又脏的XML生成器。下面的代码效果很好但是当我打开它时它有额外的双引号,即<"某些">将转变为"<"""">。有没有办法用下面的代码删除它们?我不能使用xlTextPrinter,因为有些单元格的字符超过255个。任何帮助将非常感谢!

Sub test()
    Dim Desktop As String
    Dim FileName As String
    Desktop = CreateObject("Wscript.Shell").Specialfolders("Desktop")
    With ActiveSheet
        FileName = .Range("B1").Value
        .Range("H2:K33").Copy
        Workbooks.Add
        ActiveWorkbook.Sheets(1).Range("A1").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
    End With
    With ActiveWorkbook
        .SaveAs FileName:=Desktop & Application.PathSeparator & FileName  _
            , FileFormat:=xlTextMSDOS, CreateBackup:=False
        .Close SaveChanges:=False
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

因为在导出为文本时,excel会在某些情况下为文件添加引号,因此您有几个选项。

//formId - form where selectbox is //name - attribute "name" of selectbox //dataSourceUrl - url to PHP-file //affectingField - string with value that filters the selecbox's data function loadSelectbox( formId, name, dataSourceUrl, affectingField ){ //console.log('Loading data to selectbox name="'+name+'":'); var selectbox = $('#'+formId+' select[name="'+name+'"]'); if(selectbox){ //console.log("Selecbox found"); if(affectingField != null){ var affectingValue = $('#'+formId+' [name="'+affectingField+'"]').val(); dataSourceUrl += '?affectingValue='+affectingValue; } var options = selectbox.find('option'); var jqxhr = $.ajax({ url: dataSourceUrl, dataType: 'text' }) .done(function(data) { //console.log(data); if(data != ""){ var optionsObject = JSON.parse(data); var i = 0; console.log(optionsObject); var options = []; $(optionsObject).each( function(){ options[i] = '<option value="'+$(this)[0]['val']+'">'+$(this)[0]['text']+'</option>'; i++; } ); selectbox.html(options); if(urlParamsSet[name] == false){ setParamFromUrl(name); } } else{ selectbox.html('<option value="">Все</option>'); } }) .fail(function() { alert("Problems with server answer"); }) selectbox.prop("disabled", false); } else{ console.log("No selectbox with such name"); } 是标签

引用

Chr(9)

Chr(34)是一个换行符号

您可以直接将文件写入文本,而不是保存文件:

vbCrLf

或者像Siddarth Rout建议您在创建文件后打开文件并替换它添加的其他引号:

Dim Desktop As String
Dim FileName As String
Dim fs As Object
Dim f As Object
Dim i As Integer

Set fs = CreateObject("Scripting.FileSystemObject")

Desktop = CreateObject("Wscript.Shell").Specialfolders("Desktop")
With ActiveSheet
    FileName = Desktop & Application.PathSeparator & .Range("B1").Value
    Set f = fs.createTextFile(FileName & ".txt", True, False)

    For i = 2 To 33
        f.write (.Cells(i, 8) & Chr(9) & .Cells(i, 9) & Chr(9) & .Cells(i, 10) & Chr(9) & .Cells(i, 11) & vbCrLf)
    Next

    f.Close
    Set f = Nothing
    Set fs = Nothing
End With